题目链接 | 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_007f = [1] + [0] * highfor i in range(1, high+1):if i >= zero: f[i] = (f[i] + f[i-zero]) % MODif i >= one: f[i] = (f[i] + f[i-one]) % MODreturn sum(f[low:]) % MOD