-
-
Notifications
You must be signed in to change notification settings - Fork 195
[i-mprovising] Week 01 solutions #1167
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
6 commits
Select commit
Hold shift + click to select a range
c9e321d
week 1 solution
i-mprovising 36e2ae8
fix linelint
i-mprovising a6917bb
fix linelint
i-mprovising 41dce17
fix linelint
i-mprovising 7d75381
Week 1 solutions
i-mprovising 021e051
remove except week 1 solutions
i-mprovising 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,11 @@ | ||
""" | ||
O(n) complexity | ||
""" | ||
|
||
|
||
class Solution: | ||
def containsDuplicate(self, nums: List[int]) -> bool: | ||
nums_set = set(nums) | ||
if len(nums) == len(nums_set): | ||
return False | ||
return True | ||
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,21 @@ | ||
""" | ||
Time complexity O(n) | ||
""" | ||
|
||
class Solution: | ||
def rob(self, nums: List[int]) -> int: | ||
n = len(nums) | ||
if n == 1: | ||
return nums[0] | ||
dp = [nums[0], max([nums[0], nums[1]])] | ||
if n == 2: | ||
return dp[1] | ||
|
||
for i in range(2, n): | ||
num = nums[i] | ||
tmp = [ | ||
dp[i-2] + num, | ||
dp[i-1] | ||
] | ||
dp.append(max(tmp)) | ||
return dp[-1] |
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 @@ | ||
""" | ||
Time complexity O(n) | ||
""" | ||
|
||
class Solution: | ||
def longestConsecutive(self, nums: List[int]) -> int: | ||
num_set = set(nums) # hash table | ||
longest = 0 | ||
|
||
for n in num_set: | ||
if n-1 in num_set: | ||
continue | ||
cur_len = 1 | ||
while n + cur_len in num_set: | ||
cur_len += 1 | ||
|
||
longest = max(longest, cur_len) | ||
|
||
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,11 @@ | ||
class Solution: | ||
def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
"""O(nlogn) complexity""" | ||
frequency = defaultdict(int) | ||
for n in nums: | ||
frequency[n] += 1 | ||
sorted_frequency = sorted(frequency.items(), key=lambda x:x[1], reverse=True) | ||
answer = [] | ||
for i in range(k): | ||
answer.append(sorted_frequency[i][0]) | ||
return answer |
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,12 @@ | ||
""" | ||
hash table | ||
O(n) complexity | ||
""" | ||
|
||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
table = {num:idx for idx, num in enumerate(nums)} | ||
for i, x in enumerate(nums): | ||
y = target - x | ||
if (y in table) and table[y] != i: | ||
return [i, table[y]] | ||
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. 첫 번째 루프에서 table을 미리 만드는 것보다는, enumerate로 순회하면서 동시에 dict에 저장해서 ex) table = {}
for i, num in enumerate(nums):
complement = target - num
if complement in table:
return [table[complement], i]
table[num] = i 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. 상세한 리뷰 감사합니다! |
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을 이용해서 중복을 없앤뒤 기존 배열과 비교해서 처리한 부분이 저랑 비슷한 풀이같네요👍