https://youtu.be/6aEyTjOwlJU?si=gOHrfB_eRCdonSuR
class Solution:
def numDecodings(self, s: str) -> int:
dp = {len(s) : 1}
def dfs(i):
if i in dp:
return dp[i]
if s[i] == "0":
return 0
res = dfs(i+1)
if (i+1 < len(s) and (s[i] == "1" or s[i] == "2" and s[i+1] in "0123456")):
res += dfs(i+2)
dp[i] = res
return res
return dfs(0)
class Solution:
def numDecodings(self, s: str) -> int:
dp = {len(s) : 1}
for i in range(len(s)-1, -1, -1):
if s[i] == "0":
dp[i] = 0
else:
dp[i] = dp[i+1]
if (i+1 < len(s) and (s[i] == "1" or s[i] == "2" and s[i+1] in "0123456")):
dp[i] += dp[i+2]
return dp[0]
https://youtu.be/yzBybZnvrJ8?si=Dp9Iqgu2SZJHsbZE
'LeetCode > DP심화' 카테고리의 다른 글
256. Paint House (0) | 2025.01.13 |
---|---|
983. Minimum Cost For Tickets (0) | 2025.01.13 |
2466. Count Ways To Build Good Strings (0) | 2025.01.13 |
2140. Solving Questions With Brainpower (0) | 2025.01.12 |
474. Ones and Zeroes (0) | 2025.01.12 |