From ed7e3aa68a8551c26df18c675a2862e7d0e5eeb5 Mon Sep 17 00:00:00 2001 From: hypoxisaurea Date: Wed, 23 Jul 2025 14:05:35 +0900 Subject: [PATCH 1/4] Solve two-sum --- two-sum/hypoxisaurea.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 two-sum/hypoxisaurea.py diff --git a/two-sum/hypoxisaurea.py b/two-sum/hypoxisaurea.py new file mode 100644 index 000000000..22975a4bd --- /dev/null +++ b/two-sum/hypoxisaurea.py @@ -0,0 +1,15 @@ +from typing import List + +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + lookup = {} + + for i, num in enumerate(nums): + diff = target - num + + if diff in lookup: + return [lookup[diff], i] + + lookup[num] = i + + raise ValueError('No answer') \ No newline at end of file From 990065d8742827ef7f2a73da91ad4909aa264fa7 Mon Sep 17 00:00:00 2001 From: hypoxisaurea Date: Wed, 23 Jul 2025 14:05:49 +0900 Subject: [PATCH 2/4] Add solutions --- two-sum/hypoxisaurea.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/two-sum/hypoxisaurea.py b/two-sum/hypoxisaurea.py index 22975a4bd..22e23243c 100644 --- a/two-sum/hypoxisaurea.py +++ b/two-sum/hypoxisaurea.py @@ -1,6 +1,18 @@ from typing import List class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + for i in range(len(nums)): + diff = target - nums[i] + + for j in range(i+1, len(nums)): + if nums[j] == diff: + return [i, j] + + raise ValueError('No answer') + + +class Solution2: def twoSum(self, nums: List[int], target: int) -> List[int]: lookup = {} From 1c2b4780a41345c912ca1fbbd984249ceaed2003 Mon Sep 17 00:00:00 2001 From: hypoxisaurea Date: Wed, 23 Jul 2025 14:10:55 +0900 Subject: [PATCH 3/4] Add solutions --- contains-duplicate/hypoxisaurea.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 contains-duplicate/hypoxisaurea.py diff --git a/contains-duplicate/hypoxisaurea.py b/contains-duplicate/hypoxisaurea.py new file mode 100644 index 000000000..fed4ae241 --- /dev/null +++ b/contains-duplicate/hypoxisaurea.py @@ -0,0 +1,13 @@ +from typing import List + +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + num_set = set() + + for num in nums: + if num in num_set: + return True + else: + num_set.add(num) + + return False \ No newline at end of file From de3b81bf6edb09ad1e73a3837a3754ca6f9d2b52 Mon Sep 17 00:00:00 2001 From: hypoxisaurea Date: Wed, 23 Jul 2025 14:19:14 +0900 Subject: [PATCH 4/4] Add Solutions --- contains-duplicate/hypoxisaurea.py | 20 ++++++++++++++++++++ two-sum/hypoxisaurea.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/contains-duplicate/hypoxisaurea.py b/contains-duplicate/hypoxisaurea.py index fed4ae241..908abd57d 100644 --- a/contains-duplicate/hypoxisaurea.py +++ b/contains-duplicate/hypoxisaurea.py @@ -1,3 +1,23 @@ + +''' +풀이: + 배열 nums 에 중복된 값이 있는지 효율적으로 검사하기 위해 해시 집합(Hash Set) 사용 + + 빈 집합 num_set + 배열의 각 요소 num을 순회하며: + 만약 num이 이미 num_set에 들어있다면 → 중복이므로 즉시 True 반환 + 아니라면 num_set에 num을 추가하고 다음 요소로 넘어감 + 순회를 모두 마쳤음에도 중복을 발견하지 못했다면 False 반환 + +시간 복잡도: O(n) + 배열을 한 번 순회하며, 각 요소마다 집합에 대한 조회(in)와 삽입(add) 연산 수행 + 파이썬 set의 평균 조회·삽입 비용은 O(1)이므로 전체 O(n) + +공간 복잡도: O(n) + 최악의 경우 배열의 모든 요소가 중복 없이 집합에 저장되므로, 추가로 n개의 공간을 사용 +''' + + from typing import List class Solution: diff --git a/two-sum/hypoxisaurea.py b/two-sum/hypoxisaurea.py index 22e23243c..a9cd31597 100644 --- a/two-sum/hypoxisaurea.py +++ b/two-sum/hypoxisaurea.py @@ -1,3 +1,33 @@ +''' +1. Brute Force 풀이: + 배열의 각 위치 i를 기준으로 diff = target - nums[i] 를 계산하고 + 그 뒤쪽(i+1부터 끝까지) 요소들 중 nums[j] == diff 인 j를 찾으면 + [i, j] 반환 + +시간 복잡도: O(n²) + 이중 루프를 돌며 모든 쌍을 검사 + +공간 복잡도: O(1) + 추가 자료구조를 사용하지 않고 상수 공간만 소모 + + +2. Hash Map 풀이: + 빈 딕셔너리 lookup = {} 준비 + 배열을 순회하며 각 요소 num에 대해 diff = target - num 을 계산 + + if diff in lookup: + 보수가 이미 맵에 있으면 즉시 [lookup[diff], i] 반환 + 그렇지 않으면 lookup[num] = i 로 현재 값과 인덱스를 기록 + 순회 종료 후에도 못 찾으면 에러 발생 + +시간 복잡도: O(n) +한 번 순회하며 딕셔너리 조회·삽입이 평균 O(1) + +공간 복잡도: O(n) +최악의 경우 모든 요소를 맵에 저장 +''' + + from typing import List class Solution: