Repository navigation
[wlghsp] WEEK 01 Solutions #2921
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
Changes from all commits
0044360
b67fc36
23c4d21
5ca7354
308241d
e7439db
360d8af
61fee06
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,7 @@ | ||
| """ | ||
| * Time Complexity: O(N) | ||
| * Space Complexity: O(N) | ||
| """ | ||
| class Solution: | ||
| def containsDuplicate(self, nums: list[int]) -> bool: | ||
| return len(set(nums)) != len(nums) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| """ | ||
| * Time Complexity: O(N) | ||
| * Space Complexity: O(N) | ||
| """ | ||
| class Solution: | ||
| def rob(self, nums: list[int]) -> int: | ||
| n = len(nums) | ||
|
|
||
| if n == 1: | ||
| return nums[0] | ||
|
|
||
| # dp[i]: 0번 부터 i번째 집까지 고려했을 때 얻을 수 있는 최대 금액 | ||
| dp = [0] * n | ||
| dp[0] = nums[0] | ||
| dp[1] = max(nums[0], nums[1]) | ||
|
|
||
| for i in range(2, n): | ||
| dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]) | ||
|
|
||
| return dp[-1] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| """ | ||
| * Time Complexity: O(N) | ||
| * Space Complexity: O(N) | ||
| """ | ||
| class Solution: | ||
| def longestConsecutive(self, nums: list[int]) -> int: | ||
| nums_set = set(nums) | ||
| ans = 0 | ||
| for n in nums_set: | ||
| if (n - 1) not in nums_set: | ||
| length = 1 | ||
| while n + length in nums_set: | ||
| length += 1 | ||
| ans = max(length, ans) | ||
| return ans |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| """ | ||
| * Time Complexity: O(n) | ||
| * Space Complexity: O(n) | ||
| """ | ||
| from collections import Counter | ||
| class Solution: | ||
| def topKFrequent(self, nums: list[int], k: int) -> list[int]: | ||
| # 1. 빈도 계산: O(N) | ||
| count = Counter(nums) | ||
|
|
||
| # 2. 빈도를 인덱스로 사용하는 버킷 배열 생성: O(N) | ||
| # 최대 빈도는 len(nums)이므로 크기는 len(nums) + 1 | ||
| buckets = [[] for _ in range(len(nums) + 1)] | ||
|
Contributor
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. 저도 문제봤을땐 처음푸신 방법 비슷하게 생각했는데 더 효율적인 방법도 있었네요. 참고하겠습니다.
Contributor
Author
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. 처음에는 heap으로 풀이했는데 O(N)으로 가능한 버킷 정렬 풀이가 있길래 한번 학습해보고 있습니다. 리뷰 감사합니다!😊 |
||
| for num, freq in count.items(): | ||
| buckets[freq].append(num) | ||
|
|
||
| # 3. 높은 빈도(뒤쪽 인덱스)부터 k개 추출: O(N) | ||
| res = [] | ||
| for freq in range(len(buckets) - 1, 0, -1): | ||
| for num in buckets[freq]: | ||
| res.append(num) | ||
| if len(res) == k: | ||
| return res | ||
| return res | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| """ | ||
| * Time Complexity: O(N) | ||
| * Space Complexity: O(N) | ||
| """ | ||
| class Solution: | ||
| def twoSum(self, nums: list[int], target: int) -> list[int]: | ||
| num_dict = {} | ||
| for i, e in enumerate(nums): | ||
| other = target - e | ||
| if other in num_dict: | ||
| return [num_dict[other], i] | ||
| num_dict[e] = i | ||
| return [] |
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.
👍 빈도를 계산해주는
Counter가 있군요. 배워갑니다. 감사합니다!Uh oh!
There was an error while loading. Please reload this page.
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.
Counter가 있지만 너무 만들어주는 느낌이라 잘 안 쓸려고 하는데 깔끔해 보여서 썼습니다. 리뷰 감사합니다! 😊