diff --git a/cpp/2305/LC230513.cpp b/cpp/2305/LC230513.cpp new file mode 100644 index 0000000..18a30aa --- /dev/null +++ b/cpp/2305/LC230513.cpp @@ -0,0 +1,38 @@ +#include + +/** + * 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: + * + * Append the character '0' zero times. + * Append the character '1' one times. + * This can be performed any number of times. + * + * A good string is a string constructed by the above process having a length between low and high (inclusive). + * + * Return the number of different good strings that can be constructed satisfying these properties. Since the answer can be large, return it modulo 109 + 7. + */ + +class LC230513 { +public: + static int countGoodStrings(int, int, int, int) noexcept; +}; + +int LC230513::countGoodStrings(int low, int high, int zero, int one) noexcept { + static constexpr int mod = 1'000'000'007; + int* dp = new int[high + 5]{1}; + for (int i = zero; i < one; ++i) + dp[i] = dp[i - zero]; + for (int i = one; i < zero; ++i) + dp[i] = dp[i - one]; + for (int i = std::max(zero, one); i <= high; ++i) + dp[i] = (dp[i - zero] + dp[i - one]) % mod; + int ret = 0; + for (int i = low; i <= high; ++i) + ret = (ret + dp[i]) % mod; + delete[] dp; + return ret; +} + +using Solution = LC230513; diff --git a/cpp/2305/defs.h b/cpp/2305/defs.h index 94a3a6c..8ca2142 100644 --- a/cpp/2305/defs.h +++ b/cpp/2305/defs.h @@ -129,4 +129,9 @@ public: static long long mostPoints(const std::vector>&) noexcept; }; +class LC230513 { +public: + static int countGoodStrings(int, int, int, int) noexcept; +}; + #endif //LEETCODE_CPP_DEFS_H diff --git a/cpp/2305/main.cpp b/cpp/2305/main.cpp index ee2c2f8..08b49b3 100644 --- a/cpp/2305/main.cpp +++ b/cpp/2305/main.cpp @@ -14,6 +14,6 @@ std::ostream& operator<<(std::ostream& os, const std::vector& x) noexcept { } int main() { - std::cout << LC230512::mostPoints({{3,2}, {4,3}, {4,4}, {2,5}}); + std::cout << LC230513::countGoodStrings(2,3,1,2); return 0; }