-
-
Notifications
You must be signed in to change notification settings - Fork 195
[rivkode] Week 2 #743
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
[rivkode] Week 2 #743
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,65 @@ | ||
from typing import List, Tuple, Set | ||
|
||
# threeSum3() | ||
# Time Complexity O(n ^ 2) | ||
# - when sorting by sorted function(TimSort) for each string it takes O(nlogn) | ||
# - traversing for loop takes O(n) and check left and right in the while loop at the same time. | ||
# Space Complexity O(n) | ||
# - when sorting takes O(1) | ||
|
||
|
||
class Solution: | ||
def threeSum(self, nums: List[int]) -> list[tuple[int, ...]]: | ||
|
||
triplets = set() | ||
for i in range(len(nums) - 2): | ||
for j in range(i + 1, len(nums)): | ||
for k in range(j + 1, len(nums)): | ||
if nums[i] + nums[j] + nums[k] == 0: | ||
triplet = [nums[i], nums[j], nums[k]] | ||
triplets.add(tuple(sorted(triplet))) | ||
|
||
return list(triplets) | ||
|
||
def threeSum2(self, nums: List[int]) -> list[tuple[int, ...]]: | ||
triplets = set() | ||
|
||
for i in range(len(nums)): | ||
seen = set() | ||
for j in range(i + 1, len(nums)): | ||
res = -(nums[i] + nums[j]) | ||
if res in seen: | ||
triplet = [nums[i], nums[j], res] | ||
triplets.add(tuple(sorted(triplet))) | ||
seen.add(nums[j]) | ||
|
||
return list(triplets) | ||
|
||
def threeSum3(self, nums: List[int]) -> list(tuple[int, ...]): | ||
triplets = set() | ||
nums.sort() | ||
|
||
for i in range(len(nums) - 2): | ||
l = i + 1 | ||
r = len(nums) - 1 | ||
|
||
while l < r: | ||
res = nums[l] + nums[r] + nums[i] | ||
if res > 0: | ||
r -= 1 | ||
elif res < 0: | ||
l += 1 | ||
elif res == 0: | ||
triple = (nums[l], nums[r], nums[i]) | ||
triplets.add(triple) | ||
l, r = l + 1, r - 1 | ||
else: | ||
raise Exception | ||
return list(triplets) | ||
|
||
if __name__ == "__main__": | ||
nums = [-1,0,1,2,-1,-4] | ||
|
||
|
||
solution = Solution() | ||
solution.threeSum3(nums) |
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,20 @@ | ||
# Time Complexity O(n) | ||
# - traversing for loop takes O(n) | ||
# Space Complexity O(n) | ||
# - appending for loop it takes O(n) | ||
|
||
class Solution: | ||
def climbStairs(self, n: int) -> int: | ||
stage = [1, 2, 3] | ||
|
||
for i in range(3, 45): | ||
value = stage[i - 1] + stage[i - 2] | ||
stage.append(value) | ||
|
||
return stage[n - 1] | ||
|
||
if __name__ == "__main__": | ||
solution = Solution() | ||
result = solution.climbStairs(5) | ||
print(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,26 @@ | ||
from typing import List | ||
|
||
# Time Complexity O(n log n) | ||
# - when sorting by sorted function(TimSort) for each string it takes O(nlogn) | ||
# - traversing for loop takes O(n) | ||
# Space Complexity O(n) | ||
# - when sorting takes O(n) | ||
|
||
class Solution: | ||
def isAnagram(self, s: str, t: str) -> bool: | ||
s_sorted = sorted(s) | ||
t_sorted = sorted(t) | ||
|
||
for i in range(len(s_sorted)): | ||
if s_sorted[i] != t_sorted[i]: | ||
return False | ||
return True | ||
|
||
if __name__ == "__main__": | ||
solution = Solution() | ||
|
||
s = "nagaram" | ||
t = "anagram" | ||
|
||
result = solution.isAnagram(s, t) | ||
print(result) | ||
Comment on lines
+20
to
+26
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. Python Standard Library 중 unittest를 사용해보시면 좋을 것 같습니다 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. 감사합니다. 앞으로의 test는 unittest를 사용하겠습니다 |
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.
만약 제가 면접관이라면 문제 제약 조건을 숙지하신 어필은 좋지만, n이 45보다 작은 경우에도 계산하게 되는 점에 대해 물어볼 것 같습니다
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.
아..! 제가 입력값에 대한 사용을 깜빡하였습니다. 수정하였습니다. 감사합니다 :)입력값에 대한 내용이 아니었군요. 음 .. 조금 더 고민해보겠습니다. n이 45보다 작으면 불필요한 연산이 추가되어보입니다.