File tree Expand file tree Collapse file tree 5 files changed +90
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 5 files changed +90
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ 시간 복잡도와 공간복잡도 추후 작성하겠습니다ㅠ
3+ 풀이 보고 하루 뒤에 기억해서 해보려고 했는데도 한참 걸렸네요
4+ """
5+ class Solution :
6+ def combinationSum (self , candidates : list [int ], target : int ) -> list [list [int ]]:
7+ ans = []
8+ comb = []
9+ def recur (n : int ):
10+ if sum (comb ) > target :
11+ return
12+ elif sum (comb ) == target :
13+ return ans .append (comb .copy ())
14+ else :
15+ for i in range (n , len (candidates )) :
16+ comb .append (candidates [i ])
17+ recur (i )
18+ comb .pop ()
19+ recur (0 )
20+ return ans
Original file line number Diff line number Diff line change 1+ """
2+ /풀이 봐도 잘 이해 못해서 추가 코멘트/
3+ nums[i]가 그 전까지 subarray의 합 total보다 작은 음수인 케이스는 어떻게 되는거지 고민했는데
4+ ex) total : -1, nums[i] = -2
5+ 어차피 -1인 시점에 maxTotal이 업데이트 됐으므로 total은 nums[i]부터 더하기 시작한다는 의미로 -2로 설정한다는 것을 깨달음
6+ 따라서 이전까지 subarray의 합만 음수 양수 체크
7+
8+ TC : for문 한번
9+ => O(N)
10+ SC : 추가적인 배열 등 메모리 쓰지 않으므로
11+ => O(1)
12+ """
13+ class Solution :
14+ def maxSubArray (self , nums : List [int ]) -> int :
15+ total = nums [0 ]
16+ maxTotal = nums [0 ]
17+ for i in range (1 , len (nums )) :
18+ if (total < 0 ) :
19+ total = nums [i ]
20+ else :
21+ total += nums [i ]
22+ maxTotal = max (total , maxTotal )
23+ return (maxTotal )
Original file line number Diff line number Diff line change 1+ """
2+ TC : for문 두번 반복하므로 O(2N)
3+ -> O(N)
4+ SC : answer 배열 외에 추가적인 메모리는 factor 변수 하나이므로
5+ -> O(1)
6+ """
7+ class Solution :
8+ def productExceptSelf (self , nums : List [int ]) -> List [int ]:
9+ answer = [1 ] * len (nums )
10+ factor = 1
11+ for i in range (len (nums ) - 1 ) :
12+ factor *= nums [i ]
13+ answer [i + 1 ] *= factor
14+ factor = 1
15+ for i in range (len (nums ) - 1 , 0 , - 1 ) :
16+ factor *= nums [i ]
17+ answer [i - 1 ] *= factor
18+ return answer
Original file line number Diff line number Diff line change 1+ """
2+ TC : n의 크기에 상관없이 32번 반복하므로
3+ O(1)
4+ SC : 추가적인 메모리 쓰지 않으므로
5+ O(1)
6+ """
7+
8+ class Solution :
9+ def reverseBits (self , n : int ) -> int :
10+ ret = 0
11+ for _ in range (31 ) :
12+ ret |= n & 1
13+ ret <<= 1
14+ n >>= 1
15+ ret |= n & 1
16+ return ret
Original file line number Diff line number Diff line change 1+ """
2+ TC : for문 내부 for문
3+ O(N^2)
4+ SC : 추가적인 메모리 쓰지 않으므로
5+ O(1)
6+ """
7+
8+ class Solution :
9+ def twoSum (self , nums : List [int ], target : int ) -> List [int ]:
10+ for i in nums :
11+ for j in nums :
12+ if i != j and nums [i ] + nums [j ] == target :
13+ return [i , j ]
You can’t perform that action at this time.
0 commit comments