Skip to content

Commit 208ccc7

Browse files
authored
Merge pull request #393 from kjb512/main
[kayden] Week 03 Solutions
2 parents 8cdb2dc + a157c44 commit 208ccc7

File tree

5 files changed

+84
-11
lines changed

5 files changed

+84
-11
lines changed

climbing-stairs/kayden.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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]

combination-sum/kayden.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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

encode-and-decode-strings/kayden.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,30 @@ class Solution:
44
# 공간복잡도: O(1)
55
def encode(self, strs):
66
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 # 문자열 크기와 실제 문자열 사이에 구분자 '#'를 추가
1110

1211
return res
1312

1413
# 시간복잡도: O(N)
1514
# 공간복잡도: O(N)
16-
def decode(self, str):
15+
def decode(self, s):
1716
idx = 0
18-
limit = len(str)
17+
limit = len(s)
1918
res = []
2019

2120
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]
2730
res.append(text)
31+
idx += num
2832

2933
return res
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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

two-sum/kayden.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 []

0 commit comments

Comments
 (0)