-
-
Notifications
You must be signed in to change notification settings - Fork 339
[Jeff] Week 02 Solutions #2402
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
Merged
Merged
[Jeff] Week 02 Solutions #2402
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0a14b4b
top k frequent elements
mrlee7 1d76061
top k frequent elements
mrlee7 3f9f72d
lcs solution
mrlee7 26ec174
valid anagram solution
mrlee7 2a9ac94
Merge branch 'DaleStudy:main' into main
mrlee7 f039b5f
valid anagram solution - add annotation
mrlee7 9698034
valid anagram solution - formatting
mrlee7 61b0e11
formatting
mrlee7 fbbbae1
climbing stairs solution
mrlee7 4652c72
3sum solution
mrlee7 dc23e61
poaes solution
mrlee7 14a2041
formatting
mrlee7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| from typing import List | ||
|
|
||
|
|
||
| class Solution: | ||
| def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
| result: List[List[int]] = [] | ||
| nums.sort() | ||
|
|
||
| for i in range(len(nums) - 2): | ||
| if i > 0 and nums[i] == nums[i - 1]: | ||
| continue | ||
|
|
||
| left = i + 1 | ||
| right = len(nums) - 1 | ||
|
|
||
| while left < right: | ||
| total = nums[i] + nums[left] + nums[right] | ||
|
|
||
| if total == 0: | ||
| result.append([nums[i], nums[left], nums[right]]) | ||
| left += 1 | ||
| right -= 1 | ||
|
|
||
| while left < right and nums[left] == nums[left - 1]: | ||
| left += 1 | ||
| while left < right and nums[right] == nums[right + 1]: | ||
| right -= 1 | ||
|
|
||
| elif total < 0: | ||
| left += 1 | ||
| else: | ||
| right -= 1 | ||
|
|
||
| return result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| class Solution: | ||
| def climbStairs(self, n: int) -> int: | ||
| stairs = {1: 1, 2: 2} | ||
|
|
||
| for i in range(3, n + 1): | ||
| stairs[i] = stairs[i - 1] + stairs[i - 2] | ||
|
|
||
| return stairs[n] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| from typing import List | ||
|
|
||
| """ | ||
| Ideation: | ||
| 배열을 정렬한 뒤, 인접한 숫자들을 비교하면서 가장 긴 연속 수열의 길이를 구한다. | ||
|
|
||
| - 같은 숫자는 중복이므로 건너뛴다. | ||
| - 현재 숫자 + 1 이 다음 숫자와 같으면 연속된 수이므로 길이를 증가시킨다. | ||
| - 연속이 끊기면 최대 길이를 갱신하고 길이를 1로 초기화한다. | ||
|
|
||
| Time Complexity: O(n log n) | ||
| Space Complexity: O(1) | ||
| """ | ||
|
|
||
|
|
||
| class Solution: | ||
|
|
||
| def longestConsecutive(self, nums: List[int]) -> int: | ||
| if len(nums) == 0: | ||
| return 0 | ||
|
|
||
| longest = 0 | ||
| length = 1 | ||
|
|
||
| nums.sort() | ||
|
|
||
| for idx in range(len(nums) - 1): | ||
| if nums[idx] == nums[idx + 1]: | ||
| continue | ||
| if nums[idx] + 1 == nums[idx + 1]: | ||
| length += 1 | ||
| else: | ||
| longest = max(longest, length) | ||
| length = 1 | ||
| longest = max(longest, length) | ||
| return longest | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| from typing import List | ||
|
|
||
|
|
||
| class Solution: | ||
| def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
| result = [0] * len(nums) | ||
|
|
||
| prefix_product = 1 | ||
| for index in range(len(nums)): | ||
| result[index] = prefix_product | ||
| prefix_product *= nums[index] | ||
|
|
||
| suffix_product = 1 | ||
| for index in range(len(nums) - 1, -1, -1): | ||
| result[index] *= suffix_product | ||
| suffix_product *= nums[index] | ||
|
|
||
| return result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| from typing import List | ||
|
|
||
| """ | ||
| Ideation | ||
| 해시맵에 각 원소별 빈도수를 저장한 뒤, 빈도수 높은 순으로 정렬한 다음 앞에서 k개를 반환합니다. | ||
| Time Complexity: O(n) | ||
| Space Complexity: O(n + m log m) | ||
| """ | ||
|
|
||
| class Solution: | ||
| def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
| frequency = {} | ||
| for num in nums: | ||
| frequency[num] = frequency.get(num, 0) + 1 | ||
| sorted_nums = sorted(frequency, key=lambda num: frequency[num], reverse=True) | ||
|
|
||
| return sorted_nums[:k] | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| class Solution: | ||
| """ | ||
| Ideation: | ||
| 애너그램은 문자열 내 배치만 다를 뿐, 동일한 문자들이 동일한 카운트로 보장되어야 한다. | ||
| 이를 위해 문자 기준으로 정렬하고 같은지 확인합니다. | ||
| Time Complexity: O(n log n) | ||
| Space Complexity: O(1) | ||
| """ | ||
|
|
||
| def isAnagram(self, s: str, t: str) -> bool: | ||
| return sorted(s) == sorted(t) | ||
|
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. 간결하네요 👍 |
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
저는 set 을 사용해서 공간복잡도를 희생했는데, 정렬도 좋네요. 배워갑니다 👍