-
-
Notifications
You must be signed in to change notification settings - Fork 245
[jungsiroo] Week 1 #684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[jungsiroo] Week 1 #684
Changes from all commits
b89edee
0c21985
e372a24
55986a0
d96b0eb
107c885
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
|
||
"""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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사소하지만 check라는 네이밍은 언뜻 set이라고 짐작하기 어려울 수 있을 것 같습니다~ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저도 햇갈렸어요 😆 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네이밍에 조금 더 신경써야겠네요! ㅎㅎ 감사합니다! |
||
|
||
return False | ||
|
||
|
||
|
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]) | ||
|
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)] | ||
|
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)) |
There was a problem hiding this comment.
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)
아닐까요?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
맞네요! 리턴한다고 헷갈렸던 것 같습니다! 감사합니다~