动态规划42(Leetcode2466统计构造好字符串的方案数)

代码:

class Solution {
    public int countGoodStrings(int low, int high, int zero, int one) {
        long MOD = 1000000007;
        int[] dp = new int[high+1];
        dp[0]=1;
        for(int i=1;i<=high;i++){
            if(i-zero>=0){
                dp[i] += dp[i-zero];
            }
            if(i-one>=0){
                dp[i] +=dp[i-one];
            }
            dp[i]%=MOD;
        }
        long res = 0;
        for(int i=low;i<=high;i++){
            res += dp[i];
        }
        res%=MOD;
        return (int)res;
    }
}

你可能感兴趣的:(动态规划,算法,java)