Skip to content

Commit 548e4cc

Browse files
authored
Merge pull request #1134 from seungriyou/main
[seungriyou] Week 01 Solutions
2 parents c469285 + 4333e4b commit 548e4cc

File tree

5 files changed

+198
-0
lines changed

5 files changed

+198
-0
lines changed

โ€Žcontains-duplicate/seungriyou.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# https://leetcode.com/problems/contains-duplicate/
2+
3+
from typing import List
4+
5+
class Solution:
6+
def containsDuplicate1(self, nums: List[int]) -> bool:
7+
"""
8+
[Complexity]
9+
- TC: O(n) (set(nums) ์‹œ ์›์†Œ๋ฅผ ๋ณต์‚ฌํ•˜๋Š” ๋ฐ์— O(n), len()์€ O(1))
10+
- SC: O(n)
11+
12+
[Approach]
13+
set(hash map)์„ ์ด์šฉํ•˜์—ฌ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๊ฒฐ๊ณผ์˜ ๊ธธ์ด๋ฅผ ์›๋ณธ ๋ฆฌ์ŠคํŠธ์˜ ๊ธธ์ด์™€ ๋น„๊ตํ•˜๋ฉด ๋œ๋‹ค.
14+
"""
15+
16+
return len(nums) != len(set(nums))
17+
18+
19+
def containsDuplicate(self, nums: List[int]) -> bool:
20+
"""
21+
[Complexity]
22+
- TC: O(n) (iteration)
23+
- SC: O(n)
24+
25+
[Approach]
26+
nums๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ set(hash map)์— ๋“ฑ์žฅํ–ˆ๋˜ ์›์†Œ๋ฅผ add ํ•˜๊ณ , ๋™์ผํ•œ ์›์†Œ๊ฐ€ ๋ฐœ๊ฒฌ๋˜๋ฉด True๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
27+
nums ์ „์ฒด๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๋™์ผํ•œ ์›์†Œ๊ฐ€ ์—†์—ˆ๋‹ค๋ฉด False๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
28+
"""
29+
30+
seen = set()
31+
32+
for num in nums: # -- O(n)
33+
if num in seen: # -- O(1)
34+
return True
35+
seen.add(num)
36+
37+
return False

โ€Žhouse-robber/seungriyou.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# https://leetcode.com/problems/house-robber/
2+
3+
from typing import List
4+
5+
class Solution:
6+
def rob(self, nums: List[int]) -> int:
7+
"""
8+
[Complexity]
9+
- TC: O(n)
10+
- SC: O(1) (space-optimized DP)
11+
12+
[Approach]
13+
์ธ์ ‘ํ•œ ๋‘ ์ง‘์„ ๋ชจ๋‘ ํ„ธ๋ฉด ์•ˆ ๋˜๋ฏ€๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด dp table์„ ๊ตฌ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.
14+
dp[i] = (์ˆœ์ฐจ์ ์œผ๋กœ) i-th house๋ฅผ ํ„ธ ๋•Œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” max amount of money
15+
= max(์ด์ „ ์ง‘์„ ํ„ธ์—ˆ์„ ๋•Œ, ์ด์ „ ์ง‘์„ ํ„ธ์ง€ ์•Š์•˜์„ ๋•Œ)
16+
= max(์ง€๊ธˆ ์ง‘์„ ํ„ธ ์ˆ˜ ์—†์„ ๋•Œ, ์ง€๊ธˆ ์ง‘์„ ํ„ธ ์ˆ˜ ์žˆ์„ ๋•Œ)
17+
= max(dp[i - 1], dp[i - 2] + nums[i])
18+
์ด๋•Œ, dp[i] ๊ฐ’์„ ์ฑ„์šฐ๊ธฐ ์œ„ํ•ด dp[i - 1]๊ณผ dp[i - 2] ๊ฐ’๋งŒ ํ•„์š”ํ•˜๋ฏ€๋กœ,
19+
O(n) space(= list)๊ฐ€ ์•„๋‹Œ O(1) space(= variables)๋กœ optimize ํ•  ์ˆ˜ ์žˆ๋‹ค.
20+
prev1 = dp[i - 1]
21+
prev2 = dp[i - 2]
22+
"""
23+
24+
prev1 = prev2 = 0
25+
26+
for num in nums:
27+
prev1, prev2 = max(prev1, prev2 + num), prev1 # -- multiple assignment
28+
29+
return prev1
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# https://leetcode.com/problems/longest-consecutive-sequence/
2+
3+
from typing import List
4+
5+
class Solution:
6+
def longestConsecutive1(self, nums: List[int]) -> int:
7+
"""
8+
[Complexity]
9+
- TC: O(n)
10+
- SC: O(n) (unique_nums)
11+
12+
[Approach]
13+
O(n) time์— ๋Œ์•„๊ฐ€์•ผ ํ•˜๋ฏ€๋กœ O(nlogn)์ธ sorting์€ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.
14+
๊ทธ๋ฆฌ๊ณ  integer์—์„œ consecutive ๋ผ๋Š” ๊ฒƒ์€ 1 ์ฐจ์ด๋ผ๋Š” ๊ฒƒ์ด๋‹ค.
15+
๋”ฐ๋ผ์„œ hash map์—์„œ ํ˜„์žฌ ๋ณด๊ณ  ์žˆ๋Š” num ๊ฐ’์— ๋Œ€ํ•ด left(= num - 1)์™€ right(= num + 1)๋ฅผ O(1) time์— ์ฐพ์•„๋ณด๋Š” ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•œ๋‹ค.
16+
์ด๋•Œ, left์™€ right๋ฅผ ์–‘์˜†์œผ๋กœ ํ™•์žฅ์‹œ์ผœ๊ฐ€๋ฉด์„œ max_length = max(max_length, right - left - 1)๋กœ ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค.
17+
"""
18+
19+
max_length = 0
20+
unique_nums = set(nums)
21+
22+
for num in nums:
23+
# num์˜ consecutive integer์ธ left, right ๊ตฌํ•˜๊ธฐ
24+
left, right = num - 1, num + 1
25+
26+
# left, right ์–‘์˜†์œผ๋กœ ํ™•์žฅํ•˜๋ฉฐ unique_nums์—์„œ ์ง€์›Œ๋‚˜๊ฐ€๊ธฐ
27+
while left in unique_nums:
28+
unique_nums.remove(left)
29+
left -= 1
30+
while right in unique_nums:
31+
unique_nums.remove(right)
32+
right += 1
33+
34+
# ํ˜„์žฌ ๋ณด๊ณ  ์žˆ๋Š” num์ด ์†ํ•˜๋Š” consecutive sequence์˜ length๋Š” (right - left - 1)
35+
max_length = max(max_length, right - left - 1)
36+
37+
# unique_nums๊ฐ€ ๋น„์—ˆ์œผ๋ฉด, ๋”์ด์ƒ ํ™•์ธํ•  ํ•„์š”๊ฐ€ ์—†์Œ
38+
if not unique_nums:
39+
break
40+
41+
return max_length
42+
43+
def longestConsecutive(self, nums: List[int]) -> int:
44+
"""
45+
[Complexity]
46+
- TC: O(n)
47+
- SC: O(n) (unique_nums)
48+
49+
[Approach]
50+
์ฒซ ๋ฒˆ์งธ ํ’€์ด์™€ ๋น„์Šทํ•˜๋‚˜, unique_nums๋ฅผ ์ˆœํšŒํ•˜๋‹ค๊ฐ€ ํ˜„์žฌ ๋ณด๊ณ  ์žˆ๋Š” num์ด ์ž์‹ ์ด ์†ํ•œ consecutive sequence์˜ ๊ฐ€์žฅ left ๊ฐ’์ผ ๋•Œ๋งŒ
51+
์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ™•์žฅํ•œ๋‹ค๋Š” ์ ์—์„œ ์•ฝ๊ฐ„ ๋‹ค๋ฅธ ํ’€์ด์ด๋‹ค.
52+
์ด๋•Œ, ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ™•์žฅ ํ›„ max_length = max(max_length, right - num)๋กœ ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค.
53+
"""
54+
55+
max_length = 0
56+
unique_nums = set(nums)
57+
58+
for num in unique_nums:
59+
# ํ˜„์žฌ ๋ณด๊ณ  ์žˆ๋Š” num์ด, ์ž์‹ ์ด ์†ํ•œ consecutive sequence์˜ ๊ฐ€์žฅ left ๊ฐ’์ด๋ผ๋ฉด,
60+
if num - 1 not in unique_nums:
61+
# ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ™•์žฅํ•˜๊ธฐ
62+
right = num + 1
63+
while right in unique_nums:
64+
right += 1
65+
66+
# ํ˜„์žฌ ๋ณด๊ณ  ์žˆ๋Š” num์ด ์ฒซ ๋ฒˆ์งธ ๊ฐ’์ธ consecutive sequence์˜ length๋Š” (right - num)
67+
max_length = max(max_length, right - num)
68+
69+
return max_length
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# https://leetcode.com/problems/top-k-frequent-elements/
2+
3+
from typing import List
4+
5+
class Solution:
6+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
7+
"""
8+
[Complexity]
9+
- TC: O(n + klogn)
10+
- SC: O(n)
11+
12+
[Approach]
13+
1. nums๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ์›์†Œ๋ณ„ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ์นด์šดํŠธํ•œ๋‹ค.
14+
2. min heap์— (-๋“ฑ์žฅ ํšŸ์ˆ˜, ์›์†Œ) ํ˜•ํƒœ์˜ tuple์„ ์ €์žฅํ•œ๋‹ค.
15+
3. k๋ฒˆ pop ํ•˜๋ฉด, k most frequent elements๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.
16+
"""
17+
import heapq
18+
from collections import Counter
19+
20+
# 0. early stop
21+
if len(nums) == k:
22+
return nums
23+
24+
# 1. ๋“ฑ์žฅ ํšŸ์ˆ˜ ์นด์šดํŠธ
25+
cnt = Counter(nums) # -- O(n)
26+
27+
# 2. min heap์— ์ €์žฅ
28+
q = [(-c, num) for num, c in cnt.items()] # -- O(n)
29+
heapq.heapify(q) # -- O(n)
30+
31+
# 3. k๋ฒˆ pop
32+
res = []
33+
for _ in range(k): # -- O(k)
34+
_, num = heapq.heappop(q) # -- O(logn)
35+
res.append(num)
36+
37+
return res

โ€Žtwo-sum/seungriyou.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# https://leetcode.com/problems/two-sum/
2+
3+
from typing import List
4+
5+
class Solution:
6+
def twoSum(self, nums: List[int], target: int) -> List[int]:
7+
"""
8+
[Complexity]
9+
- TC: O(n)
10+
- SC: O(n)
11+
12+
[Approach]
13+
ํ•ญ์ƒ ํ•˜๋‚˜์˜ ์ •๋‹ต์ด ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋ณต์žก๋„๋ฅผ O(n^2)๋ณด๋‹ค ์ค„์ด๊ธฐ ์œ„ํ•ด์„œ๋Š” hash map์— (target - num) ๊ฐ’์„ ์ €์žฅํ•จ์œผ๋กœ์จ
14+
ํ˜„์žฌ ๋ณด๊ณ ์žˆ๋Š” ๊ฐ’๊ณผ ๋”ํ–ˆ์„ ๋•Œ target์ด ๋˜๋Š” ๊ฐ’์„ ์ฐพ๋Š” ๊ณผ์ •์— ๋“œ๋Š” ๋ณต์žก๋„๋ฅผ O(1)์œผ๋กœ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.
15+
1. nums๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ, hash map์˜ key์— ํ˜„์žฌ ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€(= ํ˜„์žฌ ๊ฐ’๊ณผ ๋”ํ–ˆ์„ ๋•Œ target์ด ๋˜๋Š” ๊ฐ’์ด ์žˆ๋Š”์ง€) ํ™•์ธํ•œ๋‹ค.
16+
2. ์กด์žฌํ•œ๋‹ค๋ฉด, ํ˜„์žฌ ๊ฐ’์˜ index์™€ hash map์˜ value์— ๊ธฐ๋ก๋˜์–ด ์žˆ๋Š” index๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
17+
3. ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, hash map์— {(target - num): index}๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
18+
"""
19+
20+
remains = dict()
21+
22+
for i, num in enumerate(nums):
23+
if num in remains:
24+
return [remains[num], i]
25+
26+
remains[target - num] = i

0 commit comments

Comments
ย (0)