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

[jinah92] Week 3 #793

Merged
merged 4 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions combination-sum/jinah92.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# O(T) time, O(C^T) space
Copy link
Contributor

Choose a reason for hiding this comment

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

시간 복잡도와 공간 복잡도가 서로 바뀐 것 같은데 확인 부탁드려요!
시간 복잡도는 각 candidate마다 dfs 호출하고 있어서 O(C^T)되고, 공간 복잡도는 호출 스택의 깊이에 비례하니까 O(T)로 되어야 할 것 같아요~

class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
results, nums = [], []

def dfs(start, total):
if total > target:
return
if total == target:
results.append(nums[:])
for i in range(start, len(candidates)):
num = candidates[i]
nums.append(num)
dfs(i, total + num)
nums.pop()

dfs(0, 0)

return results
14 changes: 14 additions & 0 deletions maximum-subarray/jinah92.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
num_set = {}
result = -math.inf

for idx, num in enumerate(nums):
if idx == 0:
num_set[idx] = max(nums[0], result)
else:
num_set[idx] = max(num, num_set[idx-1] + num)
tmp_sum = num_set[idx]
result = max(result, tmp_sum)
Comment on lines +11 to +12
Copy link
Contributor

Choose a reason for hiding this comment

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

tmp_sum에서 값 저장해서 사용하는데 변수없이 바로 참조해서 사용할 수 있을 것 같아요.
result = max(result, num_set[idx])


return result
21 changes: 21 additions & 0 deletions product-of-array-except-self/jinah92.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# O(n) time, O(n) space
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
non_zero_product = math.prod(filter(lambda x: x != 0, nums))
raw_product = math.prod(nums)
zero_total = nums.count(0)

result = []

for num in nums:
if zero_total > 1:
result.append(0)
elif zero_total == 1:
if num == 0:
result.append(non_zero_product)
else:
result.append(raw_product)
Comment on lines +13 to +17
Copy link
Contributor

Choose a reason for hiding this comment

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

질문이 있습니다! raw_product 변수는 항상 0이 되지 않을까요?
지금 조건문이 0이 배열에서 1개만 존재하는 경우,
자기 자신이 0이면 => 0이 아닌 숫자 곱 추가
자기 자신이 0이 아니면 => 다른 숫자가 0이니까 곱이 항상 0

이렇게 이해했고, 그러면 변수 없이 그냥 result.append(0) 추가해도 충분할 것 같아서요!

else:
result.append(raw_product // num)

return result
14 changes: 14 additions & 0 deletions reverse-bits/jinah92.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# O(1) time, O(1) space
class Solution:
def reverseBits(self, n: int) -> int:
stack = []
while len(stack) < 32:
stack.append(n % 2)
n //=2

result, scale = 0, 1
while stack:
result += stack.pop() * scale
scale *= 2

return result
12 changes: 12 additions & 0 deletions two-sum/jinah92.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# O(n) time, O(n) space

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
num_set = {}

for idx, num in enumerate(nums):
other_num = target - num
if other_num in num_set:
return [idx, num_set[other_num]]
else:
num_set[num] = idx
Loading