-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #796 from sungjinwi/main
[suwi] Week 03
- Loading branch information
Showing
5 changed files
with
90 additions
and
0 deletions.
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] |