題目連結 | 2466. 統計構造好字串的方案數 |
---|---|
思路 | 動態規劃-“爬樓梯”變體 |
題解連結 | 爬樓梯換皮(Python/Java/C++/Go/JS/Rust) |
關鍵點 | \(dp(i) = dp(i-zero) + dp(i-one)\) |
時間複雜度 | \(O(high)\) |
空間複雜度 | \(O(high)\) |
程式碼實現:
class Solution:
def countGoodStrings(self, low: int, high: int, zero: int, one: int) -> int:
MOD = 1_000_000_007
f = [1] + [0] * high
for i in range(1, high+1):
if i >= zero: f[i] = (f[i] + f[i-zero]) % MOD
if i >= one: f[i] = (f[i] + f[i-one]) % MOD
return sum(f[low:]) % MOD