Skip to content

Commit 4803e42

Browse files
committed
feat: 1주차 medium level 문제 풀이
1 parent d62d45f commit 4803e42

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

house-robber/jinah92.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def rob(self, nums: List[int]) -> int:
3+
memo = {}
4+
5+
def getAmount(start: int) -> int:
6+
if not start < len(nums):
7+
memo[start] = 0
8+
if start in memo:
9+
return memo[start]
10+
11+
memo[start] = max(nums[start] + getAmount(start+2), getAmount(start+1))
12+
13+
return memo[start]
14+
15+
return getAmount(0)
16+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def longestConsecutive(self, nums: List[int]) -> int:
3+
result, last = 0, None
4+
candidates = []
5+
6+
for num in sorted(set(nums)):
7+
if last is None or last + 1 == num:
8+
result += 1
9+
else:
10+
candidates.append(result)
11+
result = 1
12+
last = num
13+
14+
if result is not 0:
15+
candidates.append(result)
16+
17+
return max(candidates) if len(candidates) > 0 else 0
18+

top-k-frequent-elements/jinah92.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
3+
counters = {}
4+
5+
for num in nums:
6+
if counters.get(num):
7+
counters[num] += 1
8+
else:
9+
counters[num] = 1
10+
11+
return [val[0] for val in sorted(counters.items(), key=lambda x: x[1], reverse=True)[:k]]
12+

0 commit comments

Comments
 (0)