Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions contains-duplicate/mrlee7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import List


class Solution:
"""
Ideation:
각 원소가 발견되면 hash map 에 해당하는 item 기반으로 flag를 세운다.
해당 인덱스에서 flag가 이미 켜져있다면, 앞에서 탐색된 원소이므로 True를 반환한다.
마지막까지 다 돌았는데 중복된 케이스가 없다면 False를 반환한다.
Time complexity: O(n)
Space complexity: O(n)
"""

def containsDuplicate(self, nums: List[int]) -> bool:
seen = {}
for num in nums:
if num in seen:
return True
seen[num] = 1
return False
19 changes: 19 additions & 0 deletions two-sum/mrlee7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from typing import List


class Solution:
"""
Ideation:
target = x + y -> target - x = y
x 원소를 가장 바깥의 이터레이션에서 돌면서 하나씩 대입합니다.
찾고자 하는 값이 (x,y) 쌍이므로, 두개의 인덱스를 찾기 위해 인덱스 기준으로 이터레이션을 수행합니다( enumerate 사용 가능).
y값이 찾아지면 당시의 x값의 인덱스와 함께 (index_of_x, index_of_y) 를 리스트로 반환합니다.
Time Complexity: O(N^2)
Space Complexity: O(1)
"""

def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(0, len(nums) - 1):
for j in range(i + 1, len(nums)):
if (target - nums[i]) == nums[j]:
return [i, j]