-
-
Notifications
You must be signed in to change notification settings - Fork 246
[hyogshin] WEEK 01 solutions #1723
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
36ffedd
feat: add 0217 Contains Duplicate solution
hyogshin 10ba4e0
feat: add 0219 Two Sum solution
hyogshin 8e79dd9
feat: add 0237 Top K Frequent Elements solution
hyogshin 4ff4946
feat: add 0240 Longest Consecutive Sequence solution
hyogshin 80ad178
feat: add 0264 House Robber solution
hyogshin 54b9019
docs: add time and space complexity
hyogshin e5950a9
docs: fix complexity comments
hyogshin 793e1f4
Merge branch 'DaleStudy:main' into main
hyogshin 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,17 @@ | ||
class Solution: | ||
def containsDuplicate(self, nums: List[int]) -> bool: | ||
if len(nums) == len(set(nums)): | ||
return False | ||
else: | ||
return True | ||
|
||
|
||
|
||
''' | ||
시간 복잡도: O(n) | ||
- set(nums)는 내부적으로 nums의 모든 원소에 대해 __hash__() 및 __eq__() 호출 -> O(n) | ||
- len() 함수는 O(1) -> 무시 | ||
|
||
공간 복잡도: O(n) | ||
- set(nums)는 nums의 원소를 모두 저장할 수 있게 공간 사용 -> 최악의 경우 O(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,25 @@ | ||
class Solution: | ||
def rob(self, nums: List[int]) -> int: | ||
dp = [0] * (len(nums)) | ||
dp[0] = nums[0] | ||
if len(nums) > 1: | ||
dp[1] = max(dp[0], nums[1]) | ||
|
||
for i in range(2, len(nums)): | ||
dp[i] = max(dp[i-2] + nums[i], dp[i-1]) | ||
|
||
return max(dp) | ||
|
||
''' | ||
시간 복잡도: O(n log n) | ||
- set() -> O(n) | ||
- sorted() -> O(n log n) | ||
- for loop -> O(n) | ||
- max() -> O(n) | ||
|
||
공간 복잡도: O(n) | ||
- set() -> O(n) | ||
- sorted list -> O(n) | ||
- rs -> O(n) | ||
- O(3n) => O(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,32 @@ | ||
class Solution: | ||
def longestConsecutive(self, nums: List[int]) -> int: | ||
if not nums: | ||
return 0 | ||
s = set(nums) | ||
nums = sorted(list(s)) | ||
rs = [] | ||
cnt = 1 | ||
|
||
for i in range(len(nums)-1): | ||
if (nums[i] + 1) == nums[i+1]: | ||
cnt += 1 | ||
else: | ||
rs.append(cnt) | ||
cnt = 1 | ||
|
||
rs.append(cnt) | ||
return max(rs) | ||
|
||
''' | ||
시간 복잡도: O(n log n) | ||
- set(nums) -> O(n) | ||
- sorted(list(s)) -> O(n log n) | ||
- for loop -> O(n) | ||
- O(2n) + O(n log n) => O(2n) 이 아니라 왜 O(n log n) 이지? | ||
|
||
공간 복잡도: O(n) | ||
- set -> O(n) | ||
- sorted() -> O(n) | ||
- rs -> O(n) | ||
- O(3n) => O(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,33 @@ | ||
class Solution: | ||
def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
plus = [0] * (10**4 + 1) | ||
minus = [0] * (10**4 + 1) | ||
|
||
for i in range(len(nums)): | ||
if nums[i] < 0: | ||
minus[-(nums[i])] += 1 | ||
else: | ||
plus[nums[i]] += 1 | ||
|
||
ans = [] | ||
for i in range(k): | ||
if max(max(minus), max(plus)) == max(plus): | ||
idx = plus.index(max(plus)) | ||
ans.append(idx) | ||
plus[idx] = 0 | ||
else: | ||
idx = minus.index(max(minus)) | ||
ans.append(-(idx)) | ||
minus[idx] = 0 | ||
|
||
return ans | ||
|
||
''' | ||
시간 복잡도: O(1) | ||
- for loop -> 보통 O(n) 이지만, 길이 10001 짜리 고정 배열 -> O(1)로 취급 가능 | ||
|
||
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. 고정 배열이라도 결국 nums를 처음 for문에서 반복하고 있으므로 O(N)이라고 생각이 드는데 어떻게 생각하시나요? |
||
공간 복잡도: 입력 제외 -> O(1), 입력 포함 -> O(n) | ||
- plus 배열: 10001 -> O(1) | ||
- minus 배열: 10001 -> O(1) | ||
- ans 배열: 길이 k -> O(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,23 @@ | ||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
rs = [] | ||
for i in range(len(nums) - 1, -1, -1): | ||
pair = target - nums[i] | ||
if pair not in nums or i == nums.index(pair): | ||
continue | ||
idx = nums.index(pair) | ||
if pair in nums: | ||
rs.append(i) | ||
rs.append(idx) | ||
break | ||
return rs | ||
|
||
''' | ||
시간 복잡도: O(n^2) | ||
- nums.index(pair) -> O(n) | ||
- for loop 안에서 nums.index(pair) 최대 2번 호출 -> O(2n^2) -> O(n^2) | ||
|
||
공간 복잡도: O(1) | ||
- rs 배열에서 number 2개 저장 -> O(1) 공간 | ||
- nums 복사나 set/dict 없음 | ||
''' |
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.
2N은 즉, N의 그래프의 배수이기 때문에 NlogN의 그래프와 비교해서 항상 작은 값을 가지게 됩니다
그래서 항상 더 큰 값을 가지는 NlogN으로 시간복잡도가 정해집니다
표기를 빅오 표기법이라, 최악의 상황으로 시간복잡도를 정해서 NlogN이 됩니다