-
-
Notifications
You must be signed in to change notification settings - Fork 358
[njngwn] WEEK 01 solutions #2649
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
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,11 @@ | ||
| class Solution: | ||
| # Time Complexity: O(nlogn), n: len(nums) | ||
| # Space Complexity: O(1) | ||
| def containsDuplicate(self, nums: List[int]) -> bool: | ||
| nums.sort() # O(nlogn) | ||
|
|
||
| for i in range(len(nums)): | ||
| if i > 0 and nums[i] == nums[i - 1]: | ||
| return True | ||
|
|
||
| return False | ||
|
Comment on lines
+1
to
+11
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. 안녕하세요! 저는 다른 방식으로 풀었는데, 이 방식으로 정렬 후 인접 숫자끼리 같을 경우를 따져도 되겠다는 생각이 듭니다. 이러면 시간복잡도 O(n), 공간복잡도 O(n)이 나오는데
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. 시간복잡도를 고려했을 때, set을 이용한 것도 좋은 방법이네요! 감사합니다! |
||
|
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. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| class Solution: | ||
| # Time Complexity: O(n), n: len(nums) | ||
| # Space Complextiy: O(1) | ||
| def rob(self, nums: List[int]) -> int: | ||
| if not nums: | ||
| return 0 | ||
| if len(nums) == 1: | ||
| return nums[0] | ||
|
|
||
| two_ago = nums[0] | ||
| one_ago = max(nums[0], nums[1]) | ||
|
|
||
| for i in range(2, len(nums)): | ||
| current = max(one_ago, two_ago + nums[i]) | ||
| two_ago, one_ago = one_ago, current | ||
|
|
||
| return one_ago |
|
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. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| class Solution: | ||
| # Time Complexity: O(nlogn), n: len(nums) | ||
| # Space Complexity: O(n), n: len(nums) | ||
| def longestConsecutive(self, nums: List[int]) -> int: | ||
| if not nums: | ||
| return 0 | ||
|
|
||
| nums = sorted(set(nums)) # O(nlogn) | ||
| longest = 1 | ||
| length = 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. length 보다 좀 더 명확하게 변수명을 설정하는건 어떨까요? |
||
|
|
||
| for i in range(1, len(nums)): | ||
| if nums[i] - nums[i-1] == 1: | ||
| length += 1 | ||
| longest = max(longest, length) | ||
| else: | ||
| length = 1 | ||
|
|
||
| return longest | ||
|
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. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| from collections import Counter | ||
|
|
||
|
|
||
| class Solution: | ||
| # Solution 1 | ||
| # Time Complexity: O(nlogn), n: len(nums) | ||
| # Space Complexity: O(n), n:len(nums) | ||
| def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
| counter = {} | ||
| for num in nums: | ||
| counter[num] = counter.get(num, 0) + 1 | ||
| sorted_nums = sorted(counter, key=lambda num: counter[num]) | ||
|
|
||
| return sorted_nums[-k:] | ||
|
Comment on lines
+9
to
+14
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. dict의 value순 정렬하고, k개 뽑을때 파이썬 문법을 사용해서 깔끔하게 잘 마무리하신것 같습니다! |
||
|
|
||
| # Solution 2 | ||
| # Time Complexity: O(n), n: len(nums) | ||
| # Space Complexity: O(n), n: len(nums) | ||
| def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
| counter = Counter(nums) | ||
| buckets = [[] for _ in range(len(nums) + 1)] | ||
|
|
||
| for num, cnt in counter.items(): | ||
| buckets[cnt].append(num) | ||
|
|
||
| sorted_nums = [item for bucket in buckets for item in bucket] | ||
|
|
||
| return sorted_nums[-k:] | ||
|
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. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| class Solution: | ||
| # Time Complexity: O(nlogn), n: len(nums) | ||
| # Space Complexity: O(1) | ||
| def containsDuplicate(self, nums: List[int]) -> bool: | ||
| nums.sort() # O(nlogn) | ||
|
|
||
| for i in range(len(nums)): | ||
| if i > 0 and nums[i] == nums[i - 1]: | ||
| return True | ||
|
|
||
| return False |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
Solution.containsDuplicate— Time: ✅ O(nlogn) → O(n log n) / Space: ✅ O(1) → O(1)풀이 2:
unknown— Time: ? / Space: ?풀이 3:
unknown— Time: ? / Space: ?