File tree 5 files changed +104
-0
lines changed
product-of-array-except-self
5 files changed +104
-0
lines changed Original file line number Diff line number Diff line change
1
+ # μ΄λ ΅λ€μγ
λ³΄κ³ νμμ΅λλ€
2
+
3
+ class Solution :
4
+ def combinationSum (self , candidates : List [int ], target : int ) -> List [List [int ]]:
5
+ ans = []
6
+ def func (cur_remain , arr , idx ):
7
+ if cur_remain == 0 :
8
+ ans .append (list (arr ))
9
+ return
10
+ elif cur_remain < 0 :
11
+ return
12
+
13
+ for i in range (idx , len (candidates )):
14
+ arr .append (candidates [i ])
15
+ func (cur_remain - candidates [i ], arr , i )
16
+ arr .pop ()
17
+
18
+ func (target , [], 0 )
19
+ return ans
20
+
Original file line number Diff line number Diff line change
1
+ """
2
+ 볡μ‘λ : μμ -> μμν μ΄μ
3
+
4
+ μκ° λ³΅μ‘λ : O(n) -> len(nums) λ§νΌ λ°λ³΅
5
+ κ³΅κ° λ³΅μ‘λ : O(n) -> len(nums) ν¬κΈ°μ λ°°μ΄ a μμ±
6
+ """
7
+ class Solution :
8
+ def maxSubArray (self , nums : List [int ]) -> int :
9
+ a = [0 ] * len (nums )
10
+ a [0 ] = nums [0 ]
11
+ for i in range (1 , len (nums )):
12
+ a [i ] = max (nums [i ], nums [i ]+ a [i - 1 ])
13
+ return max (a )
14
+
Original file line number Diff line number Diff line change
1
+ """
2
+ 볡μ‘λ : μμ -> μμν μ΄μ
3
+
4
+ μκ° λ³΅μ‘λ : O(n) -> for λ¬Έμ΄ μ¬λ¬λ² μμ§λ§, len(nums)λ§νΌ μ¬λ¬λ² λ°λ³΅νλ―λ‘ O(n)
5
+ κ³΅κ° λ³΅μ‘λ : O(n) -> len(nums)λ§νΌμ λ°°μ΄ νλκ° λ μκΈ°λ―λ‘
6
+ """
7
+ class Solution :
8
+ def productExceptSelf (self , nums : List [int ]) -> List [int ]:
9
+ zeros = 0
10
+ products = 1
11
+ ans_list = [0 ] * len (nums )
12
+
13
+ for i in range (len (nums )):
14
+ if nums [i ] == 0 :
15
+ zeros += 1
16
+ products *= nums [i ]
17
+
18
+ if zeros == 1 :
19
+ products = 1
20
+ alived_i = - 1
21
+ for i in range (len (nums )):
22
+ if nums [i ] == 0 :
23
+ alived_i = i
24
+ continue
25
+ products *= nums [i ]
26
+ ans_list [alived_i ] = products
27
+ return ans_list
28
+ elif zeros >= 2 :
29
+ return ans_list
30
+
31
+ ans_list = [products ] * len (nums )
32
+ for i in range (len (nums )):
33
+ ans_list [i ] //= nums [i ]
34
+
35
+ return ans_list
36
+
Original file line number Diff line number Diff line change
1
+ """
2
+ 볡μ‘λ : μμ -> μμν μ΄μ
3
+
4
+ μκ° λ³΅μ‘λ : O(1) -> μ΄λ€ μκ° λ€μ΄μ€λλΌλ μμλ§νΌ μ°μ°
5
+ κ³΅κ° λ³΅μ‘λ : O(1) -> μ΄λ€ μκ° λ€μ΄μ€λλΌλ μμλ§νΌ ν λΉ
6
+ """
7
+ class Solution :
8
+ def reverseBits (self , n : int ) -> int :
9
+ ans = 0
10
+ for i in range (32 ):
11
+ if n % 2 == 1 :
12
+ ans += 2 ** (31 - i )
13
+ n = n // 2
14
+ return ans
15
+
Original file line number Diff line number Diff line change
1
+ """
2
+ 볡μ‘λ : μμ -> μμν μ΄μ
3
+
4
+ μκ° λ³΅μ‘λ : O(n) -> λμ
λ리 ν€λ‘ κ²μνλ κ²μ O(1), λ°λΌμ for λ¬Έ 1κ°λ‘ O(n)
5
+ κ³΅κ° λ³΅μ‘λ : O(n) -> n μ λ§νΌ λ°λ³΅λλ©΄μ κ°μ΄ ν λΉλ¨.
6
+ """
7
+ class Solution :
8
+ def twoSum (self , nums : List [int ], target : int ) -> List [int ]:
9
+ num_dict = {}
10
+ n = len (nums )
11
+
12
+ for i in range (n ):
13
+ num_A = nums [i ]
14
+ num_B = target - num_A
15
+ if num_B in num_dict :
16
+ return [i , num_dict [num_B ]]
17
+ num_dict [num_A ] = i
18
+ return []
19
+
You canβt perform that action at this time.
0 commit comments