Skip to content

Commit 208447d

Browse files
authored
[devyulbae] WEEK 01 solutions
[devyulbae] WEEK 01 solutions
2 parents 315bcb6 + b69fc8c commit 208447d

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed

contains-duplicate/devyulbae.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
Blind75 - 1. Contains Duplicate
3+
https://leetcode.com/problems/contains-duplicate/
4+
5+
Counter를 사용한 풀이
6+
Counter 생성에 n번, 조회에 n번 -> O(n)
7+
"""
8+
from typing import List
9+
from collections import Counter
10+
11+
class Solution:
12+
def containsDuplicate(self, nums: List[int]) -> bool:
13+
counter = Counter(nums)
14+
for count in counter.values():
15+
if count > 1:
16+
return True
17+
18+
return False
19+

house-robber/devyulbae.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
blind75 - House Robber
3+
LeetCode Problem: https://leetcode.com/problems/house-robber/
4+
5+
재귀
6+
F(x) = F(x+1), F(x+2) + nums[x] 중 큰 값을 계속 선택하면 된다
7+
-> 재귀를 2번씩 호출하기 때문에 시간복잡도는 O(2^n)으로 Time Limit Exceeded 발생
8+
다음 주차 때 다시 풀어보자.
9+
dp로도 될 거 같다
10+
"""
11+
12+
from typing import List
13+
14+
class Solution:
15+
def rob(self, nums: List[int]) -> int:
16+
if not nums:
17+
return 0
18+
19+
memo = {}
20+
21+
def dfs(s, memo):
22+
if s >= len(nums):
23+
return 0
24+
if s+1 in memo:
25+
prev1 = memo[s+1]
26+
else:
27+
prev1 = dfs(s+1, memo)
28+
memo[s+1] = prev1
29+
30+
if s+2 in memo:
31+
prev2 = memo[s+2]
32+
else:
33+
prev2 = dfs(s+2, memo)
34+
memo[s+2] = prev2
35+
36+
return max(prev1, prev2 + nums[s])
37+
38+
return dfs(0, memo)
39+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Blind75 - Longest Consecutive Sequence
3+
https://leetcode.com/problems/longest-consecutive-sequence/
4+
5+
조건 : O(n) 시간복잡도
6+
1. 시작점이면 초기화
7+
2. 아니면 cnt++
8+
3. 끊기면 max 갱신
9+
"""
10+
from typing import List
11+
12+
class Solution:
13+
def longestConsecutive(self, nums: List[int]) -> int:
14+
if len(nums) == 0:
15+
return 0
16+
nums.sort()
17+
length = 1
18+
max_length = 0
19+
for i in range(len(nums)-1):
20+
if nums[i+1] == nums[i]:
21+
continue
22+
elif nums[i+1] == nums[i] + 1:
23+
length += 1
24+
else:
25+
max_length = max(max_length, length)
26+
length = 1
27+
28+
max_length = max(max_length, length)
29+
return max_length
30+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Blind75 - top-k-frequent-elements
3+
https://leetcode.com/problems/top-k-frequent-elements/
4+
"""
5+
6+
from typing import List
7+
from collections import Counter
8+
9+
class Solution:
10+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
11+
# (1,1,2,2,2,3) -> {1:1, 2:3, 3:1}
12+
num_counter = Counter(nums)
13+
# {1:1, 2:3, 3:1} -> [(2,3), (1,1), (3,1)]
14+
sorted_items = sorted(num_counter.items(), key=lambda x: x[1], reverse=True)
15+
return [item[0] for item in sorted_items[:k]]
16+

two-sum/devyulbae.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
https://leetcode.com/problems/two-sum/
3+
"""
4+
from typing import List
5+
6+
class Solution:
7+
def twoSum(self, nums: List[int], target: int) -> List[int]:
8+
for i in range(len(nums)):
9+
for j in range(i+1, len(nums)):
10+
if nums[i] + nums[j] == target:
11+
return [i, j]
12+

0 commit comments

Comments
 (0)