Skip to content

Commit

Permalink
Merge pull request #393 from kjb512/main
Browse files Browse the repository at this point in the history
[kayden] Week 03 Solutions
  • Loading branch information
kjb512 authored Sep 1, 2024
2 parents 8cdb2dc + a157c44 commit 208ccc7
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 11 deletions.
12 changes: 12 additions & 0 deletions climbing-stairs/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# ์‹œ๊ฐ„๋ณต์žก๋„: O(N)
# ๊ณต๊ฐ„๋ณต์žก๋„: O(N)
class Solution:
def climbStairs(self, n: int) -> int:
dp = [0] * (n + 1)
dp[0] = 1
dp[1] = 1

for i in range(2, n + 1):
dp[i] = dp[i - 1] + dp[i - 2]

return dp[n]
18 changes: 18 additions & 0 deletions combination-sum/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# ์‹œ๊ฐ„๋ณต์žก๋„: O(N^M)
# ๊ณต๊ฐ„๋ณต์žก๋„: O(M)
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
res = []

def dfs(total, idx, path):
if total < 0:
return
elif total == 0:
res.append(path[:])

for i in range(idx, len(candidates)):
dfs(total - candidates[i], i, path + [candidates[i]])

dfs(target, 0, [])

return res
26 changes: 15 additions & 11 deletions encode-and-decode-strings/kayden.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,30 @@ class Solution:
# ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
def encode(self, strs):
res = ""
for str in strs:
size = len(str)
res += str(size)
res += str
for s in strs:
size = len(s)
res += str(size) + "#" + s # ๋ฌธ์ž์—ด ํฌ๊ธฐ์™€ ์‹ค์ œ ๋ฌธ์ž์—ด ์‚ฌ์ด์— ๊ตฌ๋ถ„์ž '#'๋ฅผ ์ถ”๊ฐ€

return res

# ์‹œ๊ฐ„๋ณต์žก๋„: O(N)
# ๊ณต๊ฐ„๋ณต์žก๋„: O(N)
def decode(self, str):
def decode(self, s):
idx = 0
limit = len(str)
limit = len(s)
res = []

while idx < limit:
num = str[idx]
text = ""
for _ in range(num):
text += str[idx]
idx+=1
# ๋ฌธ์ž์—ด ๊ธธ์ด ํŒŒ์‹ฑ
j = idx
while s[j] != '#': # ๊ตฌ๋ถ„์ž '#'๋ฅผ ์ฐพ์•„ ๋ฌธ์ž์—ด ๊ธธ์ด๋ฅผ ์ถ”์ถœ
j += 1
num = int(s[idx:j])
idx = j + 1 # '#' ๋‹ค์Œ๋ถ€ํ„ฐ ์‹ค์ œ ๋ฌธ์ž์—ด ์‹œ์ž‘

# ์‹ค์ œ ๋ฌธ์ž์—ด ์ถ”์ถœ
text = s[idx:idx + num]
res.append(text)
idx += num

return res
18 changes: 18 additions & 0 deletions product-of-array-except-self/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# ์‹œ๊ฐ„๋ณต์žก๋„: O(N)
# ๊ณต๊ฐ„๋ณต์žก๋„: out ์ œ์™ธ์‹œ O(1)
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
n = len(nums)
out = [1] * n

prod = 1
for i in range(n - 1):
prod *= nums[i]
out[i + 1] *= prod

prod = 1
for i in range(n - 1, 0, -1):
prod *= nums[i]
out[i - 1] *= prod

return out
21 changes: 21 additions & 0 deletions two-sum/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# ์‹œ๊ฐ„๋ณต์žก๋„: O(N)
# ๊ณต๊ฐ„๋ณต์žก๋„: O(N)
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
check = {}

for idx, num in enumerate(nums):
check[num] = idx

for idx, num in enumerate(nums):
# ๋™์ผํ•œ ์ˆซ์ž ๋‘ ๊ฐœ๊ฐ€ ํ•ฉ์ณ์ ธ์„œ ๋ชฉํ‘œ๊ฐ’์ด ๋˜๋Š” ๊ฒฝ์šฐ
if num * 2 == target:
# ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์ˆซ์ž๊ฐ€ ๋ฆฌ์ŠคํŠธ์— ๋‘ ๊ฐœ ์ด์ƒ ์กด์žฌํ•  ๊ฒฝ์šฐ
if check[num] != idx:
return [idx, check[num]]
continue

if target - num in check:
return [check[num], check[target - num]]

return []

0 comments on commit 208ccc7

Please sign in to comment.