Skip to content

Commit

Permalink
[Leo] 11th Week solutions (2,3)
Browse files Browse the repository at this point in the history
  • Loading branch information
leokim0922 committed Jul 12, 2024
1 parent 9b9d38c commit 551fd84
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
16 changes: 16 additions & 0 deletions coin-change/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
result = [amount + 1] * (amount + 1)
result[0] = 0

for i in range(1, amount + 1):
for c in coins:
if i >= c:
result[i] = min(result[i], result[i - c] + 1)

if result[amount] == amount + 1:
return -1

return result[amount]

## TC: O(n * len(coins)) , SC: O(n), where n denotes amount
22 changes: 22 additions & 0 deletions decode-ways/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution:
def numDecodings(self, s: str) -> int:
if not s or s[0] == "0":
return 0

n = len(s)
dp = [0] * (n + 1)
dp[0], dp[1] = 1, 1

for i in range(2, n + 1):
one = int(s[i - 1])
two = int(s[i - 2:i])

if 1 <= one <= 9:
dp[i] += dp[i - 1]

if 10 <= two <= 26:
dp[i] += dp[i - 2]

return dp[n]

## TC: O(n), SC: O(1)

0 comments on commit 551fd84

Please sign in to comment.