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

[suwi] Week 03 #796

Merged
merged 7 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
20 changes: 20 additions & 0 deletions combination-sum/sungjinwi.py
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())
Comment on lines +10 to +13
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if sum(comb) > target :
return
elif sum(comb) == target :
return ans.append(comb.copy())
sum_comb = sum(comb)
if sum_comb > target :
return
elif sum_comb == target :
return ans.append(comb.copy())

sum의 경우, O(n)을 소요하기 때문에 2회 사용하기 보다는 변수에 저장하는 편이 좋을 것 같습니다

else :
for i in range(n, len(candidates)) :
comb.append(candidates[i])
recur(i)
comb.pop()
recur(0)
return ans
23 changes: 23 additions & 0 deletions maximum-product-subarray/sungjinwi.py
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)
18 changes: 18 additions & 0 deletions product-of-array-except-self/sungjinwi.py
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
16 changes: 16 additions & 0 deletions reverse-bits/sungjinwi.py
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
13 changes: 13 additions & 0 deletions two-sum/sungjinwi.py
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]
Loading