Skip to content

Commit 1c6ecc7

Browse files
authored
Merge pull request #1678 from jongwanra/main
[jongwanra] WEEK 01 solutions
2 parents dde5e03 + 8460438 commit 1c6ecc7

File tree

5 files changed

+334
-0
lines changed

5 files changed

+334
-0
lines changed

contains-duplicate/jongwanra.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
"""
3+
[문제]
4+
https://leetcode.com/problems/contains-duplicate/description/
5+
6+
[문제 접근 방법]
7+
Set 자료구조를 활용하여 중복 여부를 개수로 확인한다.
8+
9+
[Complexity]
10+
N: nums 길이
11+
TC: O(N)
12+
SC: O(N)
13+
"""
14+
15+
class Solution(object):
16+
def containsDuplicate(self, nums):
17+
return len(set(nums)) != len(nums)
18+

house-robber/jongwanra.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"""
2+
[Problem]
3+
https://leetcode.com/problems/house-robber/description/
4+
"""
5+
6+
class Solution(object):
7+
"""
8+
[Brain Storming]
9+
같은 날에, 인접한 집을 털면 안된다.
10+
하루에 털 수 있는 최대 금액을 구하는 문제.
11+
12+
1 <= nums.length <= 100
13+
nums.length가 최대 100이기 때문에,
14+
DFS로 문제를 접근해도 풀 수 있는 문제라고 생각한다.
15+
16+
각 집을 넣는 경우와 안넣는 경우 인접한 경우를 따져서 최대 금액을 구해보자.
17+
18+
[Complexity]
19+
Time: 집을 터는 경우, 안 터는 경우 2^100.. -> Time Limit Exceeded 발생
20+
Space: O(N) 재귀 호출 스택의 길이도 비례하여 길어짐.
21+
"""
22+
def rob1(self, nums):
23+
answer = 0
24+
visited = [False] * len(nums)
25+
26+
def dfs(depth, sum):
27+
# nonlocal nums
28+
nonlocal answer
29+
nonlocal visited
30+
31+
32+
if depth == len(nums):
33+
answer = max(answer, sum)
34+
print(sum, visited)
35+
return
36+
37+
# 다음 집을 포함한 경우
38+
if depth == 0 or not visited[depth - 1]:
39+
visited[depth] = True
40+
dfs(depth + 1, sum + nums[depth])
41+
42+
# 다음 집을 포함하지 않은 경우
43+
visited[depth] = False
44+
dfs(depth + 1, sum)
45+
46+
dfs(0, 0)
47+
return answer
48+
49+
def rob(self, nums):
50+
"""
51+
다른 사람의 풀이
52+
DFS + Memoization 기법을 활용한다.
53+
ref: https://www.algodale.com/problems/house-robber/
54+
55+
[Complexity]
56+
Time: O(N)
57+
Space: O(N)
58+
59+
"""
60+
cache = {}
61+
def dfs(depth):
62+
if depth in cache:
63+
return cache[depth]
64+
if depth >= len(nums):
65+
cache[depth] = 0
66+
return cache[depth]
67+
68+
cache[depth] = max(nums[depth] + dfs(depth + 2), dfs(depth + 1))
69+
return cache[depth]
70+
71+
return dfs(0)
72+
73+
74+
sol = Solution()
75+
print(sol.rob([1, 2, 3, 1]))
76+
print(sol.rob([2,7,9,3,1]))
77+
78+
# Edge Case
79+
print(sol.rob([1]) == 1)
80+
print(sol.rob([183,219,57,193,94,233,202,154,65,240,97,234,100,249,186,66,90,238,168,128,177,235,50,81,185,165,217,207,88,80,112,78,135,62,228,247,211]))
81+
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
"""
2+
[Problem]
3+
https://leetcode.com/problems/longest-consecutive-sequence/description/
4+
5+
[Brain Storming]
6+
O(N)으로 연속된 길이를 찾아야 한다?
7+
계수 정렬을 이용하면 가능하다. 하지만 nums[i] <= 10^9이기 떄문에 공간을 너무 많이 잡아먹는다.
8+
9+
[Plan]
10+
1. max num과 min num을 찾는다.
11+
2. set을 통해 O(1)로 빠르게 num을 찾을 수 있도록 변경한다.
12+
3. for loop를 min num ~ max num으로 순회한다
13+
3-1. 연속된 길이를 비교하며 기록한다.
14+
15+
-> Time Limit Exceeded 발생
16+
-> [0,1,2,4,8,5,6,7,9,3,55,88,77,99,999999999]
17+
18+
[Plan2]
19+
Heap 자료구조를 이용해서 구한다.
20+
nums를 전부 heapify할 경우 O(N),
21+
nums를 순회하면서 heappop을 할 경우 N * log(N)
22+
"""
23+
24+
import heapq
25+
26+
27+
class Solution(object):
28+
"""
29+
[Complexity]
30+
N: nums.length
31+
M: max_num - min_num + 1
32+
---
33+
Time: O(N + M)
34+
Space: O(N)
35+
36+
nums가 [1, 9999999] 일 경우 굉장히 비효율적인 풀이.
37+
"""
38+
39+
def longestConsecutive1(self, nums):
40+
if not nums:
41+
return 0
42+
43+
min_num = min(nums)
44+
max_num = max(nums)
45+
num_set = set(nums)
46+
47+
answer = 0
48+
49+
cur_num = min_num
50+
while cur_num <= max_num:
51+
count = 0
52+
while cur_num in num_set:
53+
count += 1
54+
cur_num += 1
55+
answer = max(answer, count)
56+
cur_num += 1
57+
58+
return answer
59+
60+
"""
61+
[Complexity]
62+
N: nums.length
63+
Time: O(N * log N)
64+
Space: O(N)
65+
"""
66+
67+
def longestConsecutive2(self, nums):
68+
if not nums:
69+
return 0
70+
heapq.heapify(nums)
71+
72+
answer = 1
73+
prev_num = heapq.heappop(nums)
74+
count = 1
75+
76+
while nums:
77+
cur_num = heapq.heappop(nums)
78+
if cur_num == prev_num:
79+
continue
80+
if cur_num - prev_num == 1:
81+
# 연속인 경우
82+
count += 1
83+
prev_num = cur_num
84+
# 연속이 아닌 경우
85+
else:
86+
count = 1
87+
prev_num = cur_num
88+
answer = max(answer, count)
89+
90+
return answer
91+
92+
"""
93+
다른 사람의 풀이
94+
참고: https://www.algodale.com/problems/longest-consecutive-sequence/
95+
96+
[Plan]
97+
1. nums를 Set에 저장한다.
98+
2. 모든 정수에 대해서 for-loop를 순회한다.
99+
2-1. 정수에서 1을 뺀 값이 Set에 있다면, 구간내 첫 번째 값이 될 수 없기 때문에 무시
100+
2-2. 없다면, 1씩 길이를 최대길이로 늘려본다.
101+
2-3. 구간내 첫 번째 값이 중복으로 들어갈 수 있는 경우를 대비하여 cache 추가
102+
3. 최대 구간의 길이와 비교한다.
103+
104+
[Complexity]
105+
N: nums.length
106+
Time: O(N)
107+
Space: O(N)
108+
"""
109+
110+
def longestConsecutive(self, nums):
111+
answer = 0
112+
num_set = set(nums)
113+
cache = set()
114+
115+
for num in nums:
116+
if num - 1 in num_set:
117+
continue
118+
if num in cache:
119+
continue
120+
121+
cache.add(num)
122+
length = 1
123+
while num + length in num_set:
124+
length += 1
125+
126+
answer = max(answer, length)
127+
return answer
128+
129+
130+
solution = Solution()
131+
# Normal Case
132+
print(solution.longestConsecutive([100, 4, 200, 1, 3, 2]) == 4)
133+
print(solution.longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1]) == 9)
134+
print(solution.longestConsecutive([1, 0, 1, 2]) == 3)
135+
136+
# Edge Case
137+
print(solution.longestConsecutive([]) == 0)
138+
print(solution.longestConsecutive([0, 1, 2, 4, 8, 5, 6, 7, 9, 3, 55, 88, 77, 99, 999999999]) == 10)
139+
print(solution.longestConsecutive([9, 1, 4, 7, 3, -1, 0, 5, 8, -1, 6]) == 7)
140+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
[Problem]
3+
https://leetcode.com/problems/top-k-frequent-elements/
4+
5+
[Brain Storming]
6+
가장 자주 사용되는 요소 k개를 반환한다.
7+
8+
1<= nums.length <= 10^5
9+
O(n^2) time complexity일 경우 시간 초과 발생
10+
11+
[Plan]
12+
1. nums를 순회하면서 Hash Table에 key:element value:count 저장한다.
13+
2. nums를 순회하면서 Heap에 (count, element)를 count를 기준으로 Max Heap 형태로 저장한다.
14+
3. Max Heap에서 k개 만큼 pop한다.
15+
16+
[Complexity]
17+
N: nums.length, K: k
18+
heapq.heapify(heap): O(N)
19+
heapq.heappop(heap): log(N)
20+
21+
Time: O(N + (K * log(N)))
22+
Space: O(N + K)
23+
* num_to_count_map: O(N)
24+
* heap: O(N)
25+
* answer: O(K)
26+
27+
28+
"""
29+
30+
import heapq
31+
32+
class Solution(object):
33+
def topKFrequent(self, nums, k):
34+
num_to_count_map = {}
35+
for num in nums:
36+
num_to_count_map[num] = num_to_count_map.get(num, 0) + 1
37+
38+
heap = []
39+
for num, count in num_to_count_map.items():
40+
heap.append((-count, num))
41+
heapq.heapify(heap)
42+
43+
answer = []
44+
for _ in range(k):
45+
negative_count, frequent_num = heapq.heappop(heap) # log(N)
46+
answer.append(frequent_num)
47+
return answer
48+
49+
solution = Solution()
50+
51+
# Normal Case
52+
print(solution.topKFrequent([1,1,1,2,2,3], 2) == [1, 2])
53+
print(solution.topKFrequent([1], 1) == [1])
54+
55+

two-sum/jongwanra.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
[Problem]
3+
https://leetcode.com/problems/two-sum/description/
4+
5+
nums.length <= 10^4 => 10,000 * 10,000 => n^2 time complexity일 경우 1초 이상 소요
6+
7+
[Plan]
8+
nums를 hash table로 만들자. (key: element, value: index)
9+
이후에 nums를 돌면서 target - current_num을 뺀 값이 hash table에 존재한 경우 return하자.
10+
그랬을 경우, O(n) 시간 복잡도를 예상한다.
11+
12+
[Complexity]
13+
N: nums length
14+
Time: O(N) => HashTable에 nums를 만큼 반복문 돌림
15+
Space: O(N) => nums length 만큼 반복문을 돌면서 hash table을 생성
16+
"""
17+
18+
class Solution(object):
19+
def twoSum(self, nums, target):
20+
num_to_index_map = dict()
21+
for index in range(len(nums)):
22+
num_to_index_map[nums[index]] = index
23+
24+
for index in range(len(nums)):
25+
num = nums[index]
26+
diff = target - num
27+
# target - num 값이 존재 하지 않을 경우 무시
28+
if not diff in num_to_index_map:
29+
continue
30+
# index가 자기 자신인 경우 무시
31+
if index == num_to_index_map[diff]:
32+
continue
33+
return [index, num_to_index_map[diff]]
34+
35+
return [0, 0]
36+
37+
38+
solution = Solution()
39+
print(solution.twoSum([3, 2, 4], 6))
40+

0 commit comments

Comments
 (0)