-
-
Notifications
You must be signed in to change notification settings - Fork 245
[BEMELON] WEEK 3 Solution #387
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 3 commits
89b1809
9024ec5
d436185
ca06932
0160a7b
c70a504
cdc0d2a
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Solution: | ||
# Space complexity: O(1) | ||
# Tiem complexity: O(n) | ||
def climbStairs(self, n: int) -> int: | ||
# dp[0] is n - 2 | ||
# dp[1] is n - 1 | ||
dp = [1, 2] | ||
|
||
if n <= 2: | ||
return dp[n - 1] | ||
|
||
for i in range(3, n + 1): | ||
# dp[n] = dp[n - 1] + dp[n - 2] | ||
# = dp[1] + dp[0] | ||
dp[(i - 1) % 2] = sum(dp) | ||
|
||
return dp[(n - 1) % 2] |
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. 혹시 v1 v2는 무슨 의미일까요?ㅎㅎ 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. 앗, Follow-up 까지 풀어보려고 버전을 나눴는데, 메소드 명을 조금 더 변경해봐야겠네요 ㅎ 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. 아 풀고 계신중이었군요 ㅎㅎ 버전이라고 예상했는데 확실하지 않아 여쭸습니다 답변 감사합니다! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
class Solution: | ||
# Space complexity: O(n) | ||
# Time complexity: O(n) | ||
def _v1(self, nums: list[int]) -> list[int]: | ||
prefix = [1] | ||
for num in nums[:-1]: | ||
prefix.append(prefix[-1] * num) | ||
|
||
reverse_nums = nums[::-1] | ||
postfix = [1] | ||
for num in reverse_nums[:-1]: | ||
postfix.append(postfix[-1] * num) | ||
postfix = postfix[::-1] | ||
|
||
return [prefix[i] * postfix[i] for i in range(len(nums))] | ||
|
||
# Space complexity: O(1) | ||
# Time complexity: O(n) | ||
def _v2(self, nums: list[int]) -> list[int]: | ||
n = len(nums) | ||
answer = [1] * n | ||
|
||
# 1. save prefix product to temp | ||
temp = 1 | ||
for i in range(1, n): | ||
temp *= nums[i - 1] | ||
answer[i] *= temp | ||
|
||
# 2. save postfix product to temp | ||
temp = 1 | ||
for i in range(n - 2, -1, -1): | ||
temp *= nums[i + 1] | ||
answer[i] *= temp | ||
|
||
return answer | ||
|
||
|
||
def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
# index -> product | ||
# 0 -> - [1, 2, 3] | ||
# 1 -> [0] - [2, 3] | ||
# 2 -> [0, 1] - [3] | ||
# 3 -> [0, 1, 2] - | ||
return self._v2(nums) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Solution: | ||
# Space complexity: O(n) | ||
# Time complexity: O(n) | ||
def twoSum(self, nums: list[int], target: int) -> list[int]: | ||
num_index = {} | ||
for curr, num in enumerate(nums): | ||
rest = target - num | ||
if rest in num_index: | ||
return [num_index[rest], curr] | ||
else: | ||
num_index[num] = curr | ||
return [0, 0] | ||
|
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.
오, 창의적이십니다!