File tree Expand file tree Collapse file tree 5 files changed +77
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 5 files changed +77
-0
lines changed Original file line number Diff line number Diff line change 1+ # 시간복잡도 : O(n * m) (n: target, m: len(candidates))
2+ # 공간복잡도 : O(n * m)
3+ class Solution :
4+ def combinationSum (self , candidates : List [int ], target : int ) -> List [List [int ]]:
5+ dp = [[] for _ in range (target + 1 )]
6+ dp [0 ] = [[]]
7+
8+ for candidate in candidates :
9+ for num in range (candidate , target + 1 ):
10+ for combination in dp [num - candidate ]:
11+ temp = combination .copy ()
12+ temp .extend ([candidate ])
13+ dp [num ].append (temp )
14+
15+ return dp [target ]
16+
Original file line number Diff line number Diff line change 1+ # 시간복잡도 : O(N)
2+ # 공간복잡도 : O(1)
3+ class Solution :
4+ def maxSubArray (self , nums : List [int ]) -> int :
5+ global_sum = nums [0 ]
6+ local_sum = nums [0 ]
7+
8+ for i in range (1 , len (nums )):
9+ local_sum = max (nums [i ], local_sum + nums [i ])
10+ global_sum = max (local_sum , global_sum )
11+
12+ return global_sum
13+
Original file line number Diff line number Diff line change 1+ # 시간복잡도: O(n)
2+ # 공간복잡도: O(n)
3+ class Solution :
4+ def productExceptSelf (self , nums : List [int ]) -> List [int ]:
5+ prefix = [1 ] * len (nums )
6+ suffix = [1 ] * len (nums )
7+ product = [1 ] * len (nums )
8+
9+ for idx in range (len (nums )):
10+ if idx == 0 :
11+ prefix [idx ] = nums [idx ]
12+ else :
13+ prefix [idx ] = prefix [idx - 1 ] * nums [idx ]
14+
15+ for idx in range (len (nums ) - 1 , - 1 , - 1 ):
16+ if idx == len (nums ) - 1 :
17+ suffix [idx ] = nums [idx ]
18+ else :
19+ suffix [idx ] = suffix [idx + 1 ] * nums [idx ]
20+
21+ for idx in range (len (nums )):
22+ if idx == 0 :
23+ product [idx ] = suffix [idx + 1 ]
24+ elif idx == len (nums ) - 1 :
25+ product [idx ] = prefix [idx - 1 ]
26+ else :
27+ product [idx ] = prefix [idx - 1 ] * suffix [idx + 1 ]
28+
29+ return product
30+
Original file line number Diff line number Diff line change 1+ # 시간복잡도: O(1) (32bit)
2+ class Solution :
3+ def reverseBits (self , n : int ) -> int :
4+ return int (bin (n )[2 :].zfill (32 )[::- 1 ], 2 )
5+
Original file line number Diff line number Diff line change 1+ # 시간복잡도 : O(n)
2+ # 공간복잡도 : O(n)
3+ class Solution :
4+ def twoSum (self , nums : List [int ], target : int ) -> List [int ]:
5+ seen = {} # {num: idx, ...}
6+
7+ for i , num in enumerate (nums ):
8+ if target - num in seen :
9+ return [seen [target - num ], i ]
10+ seen [num ] = i
11+
12+ return []
13+
You can’t perform that action at this time.
0 commit comments