Skip to content

Commit 6a357b7

Browse files
authored
[ehlung] WEEK 01 Solutions (#2924)
* contains duplicate solution * two sum solution * top k frequent elements solution * longest consecutive sequence solution * house robber solution
1 parent 5e2d660 commit 6a357b7

5 files changed

Lines changed: 81 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
n = nums์˜ ๊ธธ์ด
3+
์‹œ๊ฐ„ ๋ณต์žก๋„: O(n) - ๊ฐ ์ˆซ์ž๋ฅผ ํ•œ ๋ฒˆ์”ฉ ํ™•์ธ (set ์กฐํšŒ/์ถ”๊ฐ€๋Š” O(1))
4+
๊ณต๊ฐ„ ๋ณต์žก๋„: O(n) - ์ง€๊ธˆ๊นŒ์ง€ ๋ณธ ์ˆซ์ž๋“ค์„ ๋‹ด๋Š” set
5+
"""
6+
7+
8+
class Solution:
9+
def containsDuplicate(self, nums: list[int]) -> bool:
10+
seen = set()
11+
for num in nums:
12+
if num in seen:
13+
return True
14+
seen.add(num)
15+
return False

โ€Žhouse-robber/ehlung.pyโ€Ž

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
n = nums์˜ ๊ธธ์ด
3+
์‹œ๊ฐ„ ๋ณต์žก๋„: O(n) - ๊ฐ ์ง‘์„ ํ•œ ๋ฒˆ์”ฉ ํ™•์ธ
4+
๊ณต๊ฐ„ ๋ณต์žก๋„: O(1) - ๋ฐฐ์—ด ์—†์ด ๋ณ€์ˆ˜ ๋‘ ๊ฐœ๋งŒ ์‚ฌ์šฉ
5+
"""
6+
7+
8+
class Solution:
9+
def rob(self, nums: list[int]) -> int:
10+
# prev2: ๋‘ ์ง‘ ์ „๊นŒ์ง€์˜ ์ตœ๋Œ€ ๊ธˆ์•ก, prev1: ๋ฐ”๋กœ ์ „ ์ง‘๊นŒ์ง€์˜ ์ตœ๋Œ€ ๊ธˆ์•ก
11+
prev2, prev1 = 0, 0
12+
for money in nums:
13+
prev2, prev1 = prev1, max(prev2 + money, prev1)
14+
return prev1
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
n = nums์˜ ๊ธธ์ด
3+
์‹œ๊ฐ„ ๋ณต์žก๋„: O(n) - ์‹œ์ž‘์ ์—์„œ๋งŒ ์„ธ์–ด ๋‚˜๊ฐ€์„œ ๊ฐ ์ˆซ์ž๋Š” ์ตœ๋Œ€ ๋‘ ๋ฒˆ๋งŒ ํ™•์ธ
4+
๊ณต๊ฐ„ ๋ณต์žก๋„: O(n) - ๋ชจ๋“  ์ˆซ์ž๋ฅผ ๋‹ด๋Š” set
5+
"""
6+
7+
8+
class Solution:
9+
def longestConsecutive(self, nums: list[int]) -> int:
10+
num_set = set(nums)
11+
longest = 0
12+
13+
for num in num_set:
14+
# ์ˆ˜์—ด์˜ ์‹œ์ž‘์ ์—์„œ๋งŒ ๊ธธ์ด๋ฅผ ์„ผ๋‹ค
15+
if num - 1 not in num_set:
16+
length = 1
17+
while num + length in num_set:
18+
length += 1
19+
longest = max(longest, length)
20+
21+
return longest
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
n = nums์˜ ๊ธธ์ด, m = ์„œ๋กœ ๋‹ค๋ฅธ ์ˆซ์ž์˜ ๊ฐœ์ˆ˜
3+
์‹œ๊ฐ„ ๋ณต์žก๋„: O(n + m log m) - ๋นˆ๋„ ์„ธ๊ธฐ O(n) + ์„œ๋กœ ๋‹ค๋ฅธ ์ˆซ์ž m๊ฐœ๋ฅผ ๋นˆ๋„์ˆœ ์ •๋ ฌ O(m log m)
4+
๊ณต๊ฐ„ ๋ณต์žก๋„: O(m) - ์ˆซ์ž๋ณ„ ๋นˆ๋„๋ฅผ ๋‹ด๋Š” ๋”•์…”๋„ˆ๋ฆฌ์™€ ์ •๋ ฌ ๊ฒฐ๊ณผ
5+
"""
6+
7+
8+
class Solution:
9+
def topKFrequent(self, nums: list[int], k: int) -> list[int]:
10+
count = {}
11+
for num in nums:
12+
count[num] = count.get(num, 0) + 1
13+
14+
sorted_nums = sorted(count, key=lambda num: count[num], reverse=True)
15+
return sorted_nums[:k]

โ€Žtwo-sum/ehlung.pyโ€Ž

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
n = nums์˜ ๊ธธ์ด
3+
์‹œ๊ฐ„ ๋ณต์žก๋„: O(n) - ๊ฐ ์ˆซ์ž๋ฅผ ํ•œ ๋ฒˆ์”ฉ ํ™•์ธ (๋”•์…”๋„ˆ๋ฆฌ ์กฐํšŒ/์ €์žฅ์€ O(1))
4+
๊ณต๊ฐ„ ๋ณต์žก๋„: O(n) - ์ˆซ์ž์™€ ์ธ๋ฑ์Šค๋ฅผ ๋‹ด๋Š” ๋”•์…”๋„ˆ๋ฆฌ
5+
"""
6+
7+
8+
class Solution:
9+
def twoSum(self, nums: list[int], target: int) -> list[int]:
10+
index_of = {}
11+
for i, num in enumerate(nums):
12+
need = target - num
13+
if need in index_of:
14+
return [index_of[need], i]
15+
index_of[num] = i
16+
return []

0 commit comments

Comments
ย (0)