Skip to content
Rain Hu's Workspace
Go back

[LeetCode] 2466. Count Ways To Build Good Strings

Rain Hu

2466. Count Ways To Build Good Strings


一、題目

Given the integers zero, one, low, and high, we can construct a string by starting with an empty string, and then at each step perform either of the following:

Example 1:

Example 2:

Constraints:


二、分析

三、解題

1. DP

int countGoodStrings(int low, int high, int zero, int one) {
    vector<int> dp(high+1, 0);
    int res = 0;
    dp[0] = 1;
    int start = min(zero, one);
    for (int i = start; i <= high; i++) {
        if (i-zero >= 0) dp[i] = (dp[i] + dp[i-zero]) % modulo;
        if (i-one >= 0) dp[i] = (dp[i] + dp[i-one]) % modulo;
        if (i >= low) res = (res + dp[i]) % modulo;
    }
    return res;
}

回目錄 Catalog


Share this post on:

Previous
[LeetCode] 322. Coin Change
Next
[LeetCode] 2465. Number of Distinct Averages