-
Notifications
You must be signed in to change notification settings - Fork 126
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
[jinah92] Week 3 #793
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# O(T) time, O(C^T) space | ||
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 |
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
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.
|
||
|
||
return result |
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
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. 질문이 있습니다! 이렇게 이해했고, 그러면 변수 없이 그냥 |
||
else: | ||
result.append(raw_product // num) | ||
|
||
return result |
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 |
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 |
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.
시간 복잡도와 공간 복잡도가 서로 바뀐 것 같은데 확인 부탁드려요!
시간 복잡도는 각 candidate마다 dfs 호출하고 있어서 O(C^T)되고, 공간 복잡도는 호출 스택의 깊이에 비례하니까 O(T)로 되어야 할 것 같아요~