diff --git a/contains-duplicate/mrlee7.py b/contains-duplicate/mrlee7.py new file mode 100644 index 0000000000..6ce409b2f6 --- /dev/null +++ b/contains-duplicate/mrlee7.py @@ -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 diff --git a/two-sum/mrlee7.py b/two-sum/mrlee7.py new file mode 100644 index 0000000000..a208678160 --- /dev/null +++ b/two-sum/mrlee7.py @@ -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]