Skip to content

Commit 551fd84

Browse files
committed
[Leo] 11th Week solutions (2,3)
1 parent 9b9d38c commit 551fd84

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

coin-change/Leo.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def coinChange(self, coins: List[int], amount: int) -> int:
3+
result = [amount + 1] * (amount + 1)
4+
result[0] = 0
5+
6+
for i in range(1, amount + 1):
7+
for c in coins:
8+
if i >= c:
9+
result[i] = min(result[i], result[i - c] + 1)
10+
11+
if result[amount] == amount + 1:
12+
return -1
13+
14+
return result[amount]
15+
16+
## TC: O(n * len(coins)) , SC: O(n), where n denotes amount

decode-ways/Leo.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def numDecodings(self, s: str) -> int:
3+
if not s or s[0] == "0":
4+
return 0
5+
6+
n = len(s)
7+
dp = [0] * (n + 1)
8+
dp[0], dp[1] = 1, 1
9+
10+
for i in range(2, n + 1):
11+
one = int(s[i - 1])
12+
two = int(s[i - 2:i])
13+
14+
if 1 <= one <= 9:
15+
dp[i] += dp[i - 1]
16+
17+
if 10 <= two <= 26:
18+
dp[i] += dp[i - 2]
19+
20+
return dp[n]
21+
22+
## TC: O(n), SC: O(1)

0 commit comments

Comments
 (0)