diff --git a/contains-duplicate/Chanz82.py b/contains-duplicate/Chanz82.py new file mode 100644 index 0000000000..55fcc66236 --- /dev/null +++ b/contains-duplicate/Chanz82.py @@ -0,0 +1,10 @@ +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + hashmap = dict() + sorted(nums) + for num in nums: + if hashmap.get(num, False) == True: + return True + else : + hashmap[num] = True + return False diff --git a/two-sum/Chanz82.py b/two-sum/Chanz82.py new file mode 100644 index 0000000000..55ef11f80e --- /dev/null +++ b/two-sum/Chanz82.py @@ -0,0 +1,20 @@ +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + + ## 공간 복잡도 : O(n) + ## - n 개수 만큼의 hashmap 공간만을 사용하므로 공간복잡도가 O(n)이라고 생각합니다. + + ## 시간 복잡도 : O(n) + ## - hashmap에 값과 idx를 매칭하는 작업을 n번 수행합니다. + ## - 이후 다시 nums를 순회하면서 target이 되는 짝을 찾습니다. + ## - 따라서 Worst N번 이므로, 2n = O(n)이라고 생각합니다. + + hashmap = dict() + + for idx, num in enumerate(nums): + possible_match = target - num + if possible_match in hashmap: + return [idx, hashmap[possible_match]] + else: + hashmap[num] = idx +