Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions climbing-stairs/bemelon.py
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오, 창의적이십니다!


return dp[(n - 1) % 2]
46 changes: 46 additions & 0 deletions product-of-array-except-self/bemelon.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

혹시 v1 v2는 무슨 의미일까요?ㅎㅎ

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗, Follow-up 까지 풀어보려고 버전을 나눴는데, 메소드 명을 조금 더 변경해봐야겠네요 ㅎ

Copy link
Contributor

Choose a reason for hiding this comment

The 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)


13 changes: 13 additions & 0 deletions two-sum/bemelon.py
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]