-
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
[suwi] Week 03 #796
Merged
+90
−0
Merged
[suwi] Week 03 #796
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e2a74d0
reverse-bits solution
sungjinwi 2064257
two-sum solution
sungjinwi 1242b3e
two-sum complexity
sungjinwi 549c2d8
reverse-bit complexity
sungjinwi b55cdae
product-of-array-except-self solution & complexity
sungjinwi 2423497
maximum-product-subarray solution & complexity
sungjinwi 626fc9f
combination-sum solution
sungjinwi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
""" | ||
시간 복잡도와 공간복잡도 추후 작성하겠습니다ㅠ | ||
풀이 보고 하루 뒤에 기억해서 해보려고 했는데도 한참 걸렸네요 | ||
""" | ||
class Solution: | ||
def combinationSum(self, candidates: list[int], target: int) -> list[list[int]]: | ||
ans = [] | ||
comb = [] | ||
def recur(n : int): | ||
if sum(comb) > target : | ||
return | ||
elif sum(comb) == target : | ||
return ans.append(comb.copy()) | ||
else : | ||
for i in range(n, len(candidates)) : | ||
comb.append(candidates[i]) | ||
recur(i) | ||
comb.pop() | ||
recur(0) | ||
return ans |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
""" | ||
/풀이 봐도 잘 이해 못해서 추가 코멘트/ | ||
nums[i]가 그 전까지 subarray의 합 total보다 작은 음수인 케이스는 어떻게 되는거지 고민했는데 | ||
ex) total : -1, nums[i] = -2 | ||
어차피 -1인 시점에 maxTotal이 업데이트 됐으므로 total은 nums[i]부터 더하기 시작한다는 의미로 -2로 설정한다는 것을 깨달음 | ||
따라서 이전까지 subarray의 합만 음수 양수 체크 | ||
|
||
TC : for문 한번 | ||
=> O(N) | ||
SC : 추가적인 배열 등 메모리 쓰지 않으므로 | ||
=> O(1) | ||
""" | ||
class Solution: | ||
def maxSubArray(self, nums: List[int]) -> int: | ||
total = nums[0] | ||
maxTotal = nums[0] | ||
for i in range(1, len(nums)) : | ||
if (total < 0) : | ||
total = nums[i] | ||
else : | ||
total += nums[i] | ||
maxTotal = max(total, maxTotal) | ||
return (maxTotal) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
""" | ||
TC : for문 두번 반복하므로 O(2N) | ||
-> O(N) | ||
SC : answer 배열 외에 추가적인 메모리는 factor 변수 하나이므로 | ||
-> O(1) | ||
""" | ||
class Solution: | ||
def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
answer = [1] * len(nums) | ||
factor = 1 | ||
for i in range(len(nums) - 1) : | ||
factor *= nums[i] | ||
answer[i + 1] *= factor | ||
factor = 1 | ||
for i in range(len(nums) - 1, 0, -1) : | ||
factor *= nums[i] | ||
answer[i - 1] *= factor | ||
return answer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
""" | ||
TC : n의 크기에 상관없이 32번 반복하므로 | ||
O(1) | ||
SC : 추가적인 메모리 쓰지 않으므로 | ||
O(1) | ||
""" | ||
|
||
class Solution: | ||
def reverseBits(self, n: int) -> int: | ||
ret = 0 | ||
for _ in range(31) : | ||
ret |= n & 1 | ||
ret <<= 1 | ||
n >>= 1 | ||
ret |= n & 1 | ||
return ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
""" | ||
TC : for문 내부 for문 | ||
O(N^2) | ||
SC : 추가적인 메모리 쓰지 않으므로 | ||
O(1) | ||
""" | ||
|
||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
for i in nums : | ||
for j in nums : | ||
if i != j and nums[i] + nums[j] == target : | ||
return [i, j] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
sum의 경우, O(n)을 소요하기 때문에 2회 사용하기 보다는 변수에 저장하는 편이 좋을 것 같습니다