Skip to content
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

[kayden] Week 03 Solutions #393

Merged
merged 7 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 12 additions & 0 deletions climbing-stairs/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 시간복잡도: O(N)
# 공간복잡도: O(N)
Copy link
Contributor

Choose a reason for hiding this comment

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

아 dp 를 사용하셨군요! 공간복잡도를 O(1) 로 줄일 수도 있는데 시간 되실 때 시도해보시면 좋을 것 같습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

답변 감사합니다! 다시 보니, 변수 두개로도 가능하겠군요!

class Solution:
def climbStairs(self, n: int) -> int:
dp = [0] * (n + 1)
dp[0] = 1
dp[1] = 1

for i in range(2, n + 1):
dp[i] = dp[i - 1] + dp[i - 2]

return dp[n]
18 changes: 18 additions & 0 deletions product-of-array-except-self/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 시간복잡도: O(N)
# 공간복잡도: out 제외시 O(1)
Copy link
Contributor

Choose a reason for hiding this comment

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

오해가 없도록 out 제외시라고 붙여주셔서 좋네요!

class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
n = len(nums)
out = [1] * n

prod = 1
for i in range(n - 1):
prod *= nums[i]
out[i + 1] *= prod

prod = 1
for i in range(n - 1, 0, -1):
prod *= nums[i]
out[i - 1] *= prod

return out
21 changes: 21 additions & 0 deletions two-sum/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 시간복잡도: O(N)
# 공간복잡도: O(N)
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
check = {}

for idx, num in enumerate(nums):
check[num] = idx

for idx, num in enumerate(nums):
# 동일한 숫자 두 개가 합쳐져서 목표값이 되는 경우
if num * 2 == target:
# 그리고 그 숫자가 리스트에 두 개 이상 존재할 경우
if check[num] != idx:
return [idx, check[num]]
continue

if target - num in check:
return [check[num], check[target - num]]

return []