Skip to content

Commit

Permalink
Merge pull request #796 from sungjinwi/main
Browse files Browse the repository at this point in the history
[suwi] Week 03
  • Loading branch information
SamTheKorean authored Dec 29, 2024
2 parents f0c198f + 626fc9f commit dc26587
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
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())
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]

0 comments on commit dc26587

Please sign in to comment.