一维DP
划分型:f[] means XXXX ending with i
word break
longest valid parentheses
decode ways
Longest Valid Parentheses
不能算Hard, 但是转移方程需要考虑坐标edge case, bug free不容易
划分型类似Longest Palindromic Substring,word break思路
Decode Ways
注意corner case, any valid substring must starts with non-zero
how to handle "0000" ?
state:f[i] means num of decode ways for s.substring(i)
function:
f[i] = isValid(s.substring(i, i + 1)) && f[i + 1] + isValid(s.substring(i, i + 2)) && f[i + 2]
oneDigit != 0
10 <= twoDigits <= 26
res: f[0]
initialize:
f[n] = 0
f[n - 1] = isValid(s.substring(n - 1)) ? 1 : 0
f[n - 2] = isValid(s.substring(n - 2)) ? 1 + f[n - 1] : 0
面试还是写从dfs -> memo的思路更make sense一点
除非面试官要求optimal space complexity-> DP with rolling array
Last updated
Was this helpful?