-
-
Notifications
You must be signed in to change notification settings - Fork 245
[rivkode] WEEK 02 solutions #1758
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 |
---|---|---|
@@ -1,20 +1,41 @@ | ||
# Time Complexity O(n) | ||
# - traversing for loop takes O(n) | ||
# - same with 피보나치 | ||
# 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) | ||
def climbStairs(self, n): | ||
""" | ||
:type n: int | ||
:rtype: int | ||
""" | ||
|
||
return stage[n - 1] | ||
map = {1:1, 2:2} | ||
|
||
|
||
for i in range(3, n + 1): | ||
map[i] = map[i-1] + map[i-2] | ||
|
||
return map[n] | ||
|
||
|
||
# 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) | ||
|
||
|
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
class Solution(object): | ||
def productExceptSelf(self, nums): | ||
""" | ||
:type nums: List[int] | ||
:rtype: List[int] | ||
""" | ||
|
||
all_multi = 1 | ||
zero_count = 0 | ||
result = [] | ||
|
||
for v in nums: | ||
if v == 0: | ||
zero_count += 1 | ||
continue | ||
all_multi *= v | ||
all_multi = int(all_multi) | ||
|
||
if zero_count == 0: | ||
for v in nums: | ||
if v == 0: | ||
result.append(all_multi) | ||
else: | ||
cur = all_multi / v | ||
result.append(int(cur)) | ||
elif zero_count == 1: | ||
Comment on lines
+24
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. 안녕하세요, 이쪽에서 보고 왔습니다 ㅎㅎ #1757 (comment) 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. 아하 ..! 사실,, 저는 저 나눗셈이 영어로 무엇인지 잘 몰랐습니다 .. ㅎㅎ divide가 나누다 이고 operation은 실행 동작 이라고만 알았네요 ㅋㅋㅋ 나눗셈 = division operation 도 함께 배우고 갑니다! 저는 처음 output만 보고 0이라는 특이 케이스가 존재해서 이걸 보고 시도하고 풀어봤는데, 지금 돌이켜보니 이게 되네 ? 였네요 ㅎㅎ 친절한 설명 감사합니다! |
||
for v in nums: | ||
if v == 0: | ||
result.append(all_multi) | ||
else: | ||
result.append(0) | ||
|
||
if zero_count >= 2: | ||
return [0 for _ in range(len(nums))] | ||
|
||
return result | ||
|
||
|
||
if __name__ == "__main__": | ||
solution = Solution() | ||
# nums = [1,2,3,4] | ||
# nums = [-1,1,0,-3,3] | ||
nums = [1,2,3,4,0,0] | ||
result = solution.productExceptSelf(nums) | ||
print(result) | ||
|
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,68 @@ | ||
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) | ||
# - check len first | ||
# - make set for one of list for each values | ||
# - if there's a duplication, then value += 1. This is a number of count. | ||
# - fetch value using set.get() method. | ||
# - minus 1 and check if it is less than 0 which means it's invalid. return false directly. | ||
# 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 | ||
""" | ||
:type s: str | ||
:type t: str | ||
:rtype: bool | ||
""" | ||
s_list = list(s) | ||
t_list = list(t) | ||
|
||
if len(s_list) != len(t_list): | ||
return False | ||
|
||
t_set = {} | ||
for t_val in t_list: | ||
cur = t_set.get(t_val, 0) | ||
cur += 1 | ||
t_set[t_val] = cur | ||
|
||
|
||
flag = True | ||
|
||
for s_val in s_list: | ||
if t_set.get(s_val): | ||
cur = t_set.get(s_val) | ||
cur -= 1 | ||
t_set[s_val] = cur | ||
if cur < 0: | ||
flag = False | ||
break | ||
else: | ||
flag = False | ||
break | ||
|
||
return flag | ||
|
||
# previous logic | ||
# 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" | ||
s = "a" | ||
t = "ab" | ||
|
||
result = solution.isAnagram(s, t) | ||
print(result) | ||
|
||
|
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.
혹시 리트코드에서 본 답안 코드를 제출해보셨나요? 이렇게 중복된 값이 여러 번 등장할 때 인덱스를 덮어쓰면 필요한 조합을 놓치거나 잘못된 인덱스를 사용할 수 있을 것 같습니다.
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.
@DaleSeo 주말 일정으로 늦게 답장을 드립니다. 넵! 제출해보았습니다, pass를 하긴 했었는데 많이 느린 편이네요 .. ㅎㅎ
그러네요 .. set을 사용하게 되면 잘못된 인덱스를 사용할 수 있을 것 같습니다. 답이 pass된 이유는 문제에서 다행히 인덱스와는 크게 관련있지 않고 고유한 조합 return 하면 되기때문이었던 것 같습니다. 항상 set으로 인덱스를 사용한다면 덮어씌워지는 부분을 주의해야겠습니다. 감사합니다~!