File tree Expand file tree Collapse file tree 5 files changed +84
-11
lines changed
encode-and-decode-strings
product-of-array-except-self Expand file tree Collapse file tree 5 files changed +84
-11
lines changed Original file line number Diff line number Diff line change
1
+ # 시간복잡도: O(N)
2
+ # 공간복잡도: O(N)
3
+ class Solution :
4
+ def climbStairs (self , n : int ) -> int :
5
+ dp = [0 ] * (n + 1 )
6
+ dp [0 ] = 1
7
+ dp [1 ] = 1
8
+
9
+ for i in range (2 , n + 1 ):
10
+ dp [i ] = dp [i - 1 ] + dp [i - 2 ]
11
+
12
+ return dp [n ]
Original file line number Diff line number Diff line change
1
+ # 시간복잡도: O(N^M)
2
+ # 공간복잡도: O(M)
3
+ class Solution :
4
+ def combinationSum (self , candidates : List [int ], target : int ) -> List [List [int ]]:
5
+ res = []
6
+
7
+ def dfs (total , idx , path ):
8
+ if total < 0 :
9
+ return
10
+ elif total == 0 :
11
+ res .append (path [:])
12
+
13
+ for i in range (idx , len (candidates )):
14
+ dfs (total - candidates [i ], i , path + [candidates [i ]])
15
+
16
+ dfs (target , 0 , [])
17
+
18
+ return res
Original file line number Diff line number Diff line change @@ -4,26 +4,30 @@ class Solution:
4
4
# 공간복잡도: O(1)
5
5
def encode (self , strs ):
6
6
res = ""
7
- for str in strs :
8
- size = len (str )
9
- res += str (size )
10
- res += str
7
+ for s in strs :
8
+ size = len (s )
9
+ res += str (size ) + "#" + s # 문자열 크기와 실제 문자열 사이에 구분자 '#'를 추가
11
10
12
11
return res
13
12
14
13
# 시간복잡도: O(N)
15
14
# 공간복잡도: O(N)
16
- def decode (self , str ):
15
+ def decode (self , s ):
17
16
idx = 0
18
- limit = len (str )
17
+ limit = len (s )
19
18
res = []
20
19
21
20
while idx < limit :
22
- num = str [idx ]
23
- text = ""
24
- for _ in range (num ):
25
- text += str [idx ]
26
- idx += 1
21
+ # 문자열 길이 파싱
22
+ j = idx
23
+ while s [j ] != '#' : # 구분자 '#'를 찾아 문자열 길이를 추출
24
+ j += 1
25
+ num = int (s [idx :j ])
26
+ idx = j + 1 # '#' 다음부터 실제 문자열 시작
27
+
28
+ # 실제 문자열 추출
29
+ text = s [idx :idx + num ]
27
30
res .append (text )
31
+ idx += num
28
32
29
33
return res
Original file line number Diff line number Diff line change
1
+ # 시간복잡도: O(N)
2
+ # 공간복잡도: out 제외시 O(1)
3
+ class Solution :
4
+ def productExceptSelf (self , nums : List [int ]) -> List [int ]:
5
+ n = len (nums )
6
+ out = [1 ] * n
7
+
8
+ prod = 1
9
+ for i in range (n - 1 ):
10
+ prod *= nums [i ]
11
+ out [i + 1 ] *= prod
12
+
13
+ prod = 1
14
+ for i in range (n - 1 , 0 , - 1 ):
15
+ prod *= nums [i ]
16
+ out [i - 1 ] *= prod
17
+
18
+ return out
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
+ check = {}
6
+
7
+ for idx , num in enumerate (nums ):
8
+ check [num ] = idx
9
+
10
+ for idx , num in enumerate (nums ):
11
+ # 동일한 숫자 두 개가 합쳐져서 목표값이 되는 경우
12
+ if num * 2 == target :
13
+ # 그리고 그 숫자가 리스트에 두 개 이상 존재할 경우
14
+ if check [num ] != idx :
15
+ return [idx , check [num ]]
16
+ continue
17
+
18
+ if target - num in check :
19
+ return [check [num ], check [target - num ]]
20
+
21
+ return []
You can’t perform that action at this time.
0 commit comments