File tree Expand file tree Collapse file tree 5 files changed +142
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 5 files changed +142
-0
lines changed Original file line number Diff line number Diff line change
1
+ from typing import List
2
+
3
+
4
+ class Solution :
5
+ def combinationSum (self , candidates : List [int ], target : int ) -> List [List [int ]]:
6
+ result = []
7
+
8
+ def backtrack (start , target , current_combination ):
9
+ # μ’
λ£ μ‘°κ±΄
10
+ if target == 0 :
11
+ result .append (list (current_combination ))
12
+ return
13
+ if target < 0 :
14
+ return
15
+
16
+ # λ°±νΈλνΉ
17
+ for i in range (start , len (candidates )):
18
+ current_combination .append (candidates [i ])
19
+ backtrack (i , target - candidates [i ], current_combination ) # κ°μ μμλ₯Ό μ¬λ¬ λ² μΈ μ μλλ‘ iλ₯Ό κ·Έλλ‘ λ‘λλ€.
20
+ current_combination .pop () # λμκ°μ λ€μ μλν μ μλλ‘ μμλ₯Ό μ κ±°ν©λλ€.
21
+
22
+ backtrack (0 , target , [])
23
+
24
+ return result
25
+
26
+
27
+ # μκ° λ³΅μ‘λ: O(n^t)
28
+ # - ν보 리μ€νΈμμ κ° μ«μλ₯Ό μ νν μ μκΈ° λλ¬Έμ, nκ°μ ν보λ₯Ό μ¬μ©ν΄ tλ²μ νμμ ν μ μμ΅λλ€.
29
+ # - λ°λΌμ μ΅μ
μ κ²½μ° νμ νμλ O(n^t)λ‘ λ³Ό μ μμ΅λλ€.
30
+ #
31
+ # κ³΅κ° λ³΅μ‘λ: O(t)
32
+ # - μ¬κ· νΈμΆ μ€νμ κΉμ΄λ μ΅λ target κ°μΈ tμ λΉλ‘νλ―λ‘, κ³΅κ° λ³΅μ‘λλ O(t)μ
λλ€.
33
+ # - λν, νμ¬κΉμ§ μ νλ μ«μλ€μ μ‘°ν©μ μ μ₯νλ 곡κ°λ μ΅λ tκ°κΉμ§ μ μ₯νλ―λ‘, κ³΅κ° λ³΅μ‘λλ O(t)μ
λλ€.
Original file line number Diff line number Diff line change
1
+ from typing import List
2
+
3
+
4
+ class Solution :
5
+ def maxSubArray (self , nums : List [int ]) -> int :
6
+ largest_sum = - float ('inf' )
7
+ current_sum = - float ('inf' )
8
+
9
+ for num in nums :
10
+ current_sum = max (current_sum + num , num )
11
+ largest_sum = max (largest_sum , current_sum )
12
+
13
+ return largest_sum
14
+
15
+
16
+ # μκ° λ³΅μ‘λ: O(n)
17
+ # - nums λ°°μ΄μ ν λ² μννλ©° κ° μμμ λν΄ μ΅λ λΆλΆ λ°°μ΄ ν©μ κ³μ°νλ―λ‘ μκ° λ³΅μ‘λλ O(n)μ
λλ€.
18
+ #
19
+ # κ³΅κ° λ³΅μ‘λ: O(1)
20
+ # - μΆκ°λ‘ μ¬μ©νλ λ³μλ largest_sumκ³Ό current_sum λ κ°λΏμ΄λ―λ‘ κ³΅κ° λ³΅μ‘λλ O(1)μ
λλ€.
Original file line number Diff line number Diff line change
1
+ from typing import List
2
+
3
+
4
+ class Solution :
5
+ def productExceptSelf (self , nums : List [int ]) -> List [int ]:
6
+ n = len (nums )
7
+ result = [1 ] * n
8
+
9
+ prefix = 1
10
+ for i in range (n ):
11
+ result [i ] *= prefix
12
+ prefix *= nums [i ]
13
+
14
+ suffix = 1
15
+ for i in range (- 1 , - n - 1 , - 1 ):
16
+ result [i ] *= suffix
17
+ suffix *= nums [i ]
18
+
19
+ return result
20
+
21
+
22
+ # μκ° λ³΅μ‘λ: O(n)
23
+ # - μ
λ ₯ λ°°μ΄ numsλ₯Ό λ λ² μνν©λλ€.
24
+ #
25
+ # κ³΅κ° λ³΅μ‘λ: O(1)
26
+ # - μΆκ° 곡κ°μΌλ‘ μ¬μ©νλ λ³μλ prefixμ suffixλΏμ΄λ©°,
27
+ # μΆλ ₯ λ°°μ΄(result)μ μΆκ° 곡κ°μΌλ‘ κ³μ°νμ§ μμ΅λλ€.
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def reverseBits (self , n : int ) -> int :
3
+ # μ΄μ§μλ‘ λ³νν ν '0b' μ κ±°
4
+ binary = bin (n )[2 :]
5
+ # 32λΉνΈ κΈΈμ΄μ λ§κ² μμͺ½μ 0μ μ±μ
6
+ binary = binary .zfill (32 )
7
+ # μ΄μ§μλ₯Ό λ€μ§μ
8
+ reversed_binary = binary [::- 1 ]
9
+ # λ€μ§ν μ΄μ§μλ₯Ό μ μλ‘ λ³ννμ¬ λ°ν
10
+ return int (reversed_binary , 2 )
11
+
12
+
13
+ # μκ° λ³΅μ‘λ: O(32)
14
+ # - bin(): O(32)
15
+ # - zfill(32): O(32)
16
+ # - λ¬Έμμ΄ λ€μ§κΈ° [::-1]: O(32)
17
+ # - int(λ¬Έμμ΄, 2): O(32)
18
+ # μ΄ν©: O(32) (μμ μκ°μΌλ‘ κ°μ£Ό κ°λ₯)
19
+
20
+ # κ³΅κ° λ³΅μ‘λ: O(32)
21
+ # - μ΄μ§ λ¬Έμμ΄(binary)μ λ€μ§ν λ¬Έμμ΄(reversed_binary)μ μ μ₯νλ―λ‘ O(32).
22
+
23
+
24
+ class Solution :
25
+ def reverseBits (self , n : int ) -> int :
26
+ result = 0
27
+
28
+ for i in range (32 ):
29
+ # resultλ₯Ό μΌμͺ½μΌλ‘ 1λΉνΈ μ΄λνκ³ nμ λ§μ§λ§ λΉνΈλ₯Ό μΆκ°
30
+ result = (result << 1 ) | n & 1
31
+ # nμ μ€λ₯Έμͺ½μΌλ‘ 1λΉνΈ μ΄λ
32
+ n >>= 1
33
+
34
+ return result
35
+
36
+
37
+ # μκ° λ³΅μ‘λ: O(32)
38
+ # - λ°λ³΅λ¬Έμ΄ 32λ² μ€νλλ©° κ° μμ
(λΉνΈ μ΄λ λ° OR μ°μ°)μ O(1).
39
+ # μ΄ν©: O(32) (μμ μκ°μΌλ‘ κ°μ£Ό κ°λ₯)
40
+
41
+ # κ³΅κ° λ³΅μ‘λ: O(1)
42
+ # - μΆκ°λ‘ μ¬μ©νλ λ³μ resultμ nλ§ μ μ₯νλ―λ‘ μμ 곡κ°.
Original file line number Diff line number Diff line change
1
+ from typing import List
2
+
3
+
4
+ class Solution :
5
+ def twoSum (self , nums : List [int ], target : int ) -> List [int ]:
6
+ subtract_map = {}
7
+
8
+ for i , num in enumerate (nums ):
9
+ if num in subtract_map :
10
+ return [i , subtract_map [num ]]
11
+ else :
12
+ subtract_map [target - num ] = i
13
+
14
+
15
+ # μκ° λ³΅μ‘λ: O(n)
16
+ # - nums λ°°μ΄μ ν λ² μννλ©° κ° μμλ₯Ό νμΈνλ―λ‘ μκ° λ³΅μ‘λλ O(n)μ
λλ€.
17
+ #
18
+ # κ³΅κ° λ³΅μ‘λ: O(n)
19
+ # - μΆκ°λ‘ μ¬μ©νλ subtract_map λμ
λ리μλ μ΅μ
μ κ²½μ° nums λ°°μ΄μ λͺ¨λ μμκ° μ μ₯λλ―λ‘
20
+ # κ³΅κ° λ³΅μ‘λλ O(n)μ
λλ€.
You canβt perform that action at this time.
0 commit comments