Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions contains-duplicate/jungsiroo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
# Slow - tc : O(n) / sc : O(1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return statement 안에서 쓰이는 set(nums)의 공간복잡도를 고려하면 공간복잡도가 O(n) 아닐까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맞네요! 리턴한다고 헷갈렸던 것 같습니다! 감사합니다~


"""return len(set(nums)) != len(nums)"""

# Fast - tc : O(n) / sc : O(n)
check = set()

for num in nums:
if num in check:
return True
check.add(num)
Comment on lines +8 to +13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사소하지만 check라는 네이밍은 언뜻 set이라고 짐작하기 어려울 수 있을 것 같습니다~

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 햇갈렸어요 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네이밍에 조금 더 신경써야겠네요! ㅎㅎ 감사합니다!


return False



39 changes: 39 additions & 0 deletions house-robber/jungsiroo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Solution:
def bfs(self, nums):
from collections import deque
queue = deque()

# price, idx, robbed prev
queue.append([0, 0, False])
queue.append([0, 0, True])
ret = 0

while queue:
price, idx, prev = queue.popleft()
ret = max(ret, price)
if idx == len(nums):
continue

if prev:
queue.append([price, idx+1, False])
else:
queue.append([price, idx+1, False])
queue.append([price+nums[idx], idx+1, True])

return ret

def rob(self, nums: List[int]) -> int:
# BFS - Slow and out of memory
"""return self.bfs(nums)"""

# DP
n = len(nums)
record = [[0]*n for _ in range(2)]
record[1][0] = nums[0]

for i in range(1, n):
record[1][i] = max(record[0][i-1]+nums[i], record[1][i])
record[0][i] = max(record[1][i-1], record[0][i-1])

return max(record[1][-1], record[0][-1])

26 changes: 26 additions & 0 deletions top-k-frequent-elements/jungsiroo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
# Naive Solution
# TC : O(nlogn)
# SC : O(n)

cnt = dict()
for num in nums:
cnt[num] = cnt.get(num, 0) + 1

"""
ret = dict(sorted(cnt.items(), key=lambda x:(-x[1], x[0])))
return list(ret.keys())[:k]
"""

# Follow up Solution
# TC : O(nlog(k))
# SC : O(n)

import heapq

ret = [(-c, num) for num, c in cnt.items()]
heapq.heapify(ret)

return [heapq.heappop(ret)[1] for _ in range(k)]

7 changes: 7 additions & 0 deletions valid-palindrome/jungsiroo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import re

class Solution:
def isPalindrome(self, s: str) -> bool:
# Fastest - tc : O(n) / sc : O(1
s = ''.join(re.findall(r'[a-z0-9]+', s.lower()))
return s == ''.join(reversed(s))
Loading