From 0e48212d57690b5da64cb9920bc33361f8a7c230 Mon Sep 17 00:00:00 2001 From: rain84 <rainy_sky@mail.ru> Date: Tue, 31 Dec 2024 00:55:37 +0300 Subject: [PATCH] feat: add solutions to lc problem: No.2466 --- .../README.md | 50 +++++++++++++++++ .../README_EN.md | 56 ++++++++++++++++++- .../Solution2.js | 15 +++++ .../Solution2.ts | 15 +++++ 4 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 solution/2400-2499/2466.Count Ways To Build Good Strings/Solution2.js create mode 100644 solution/2400-2499/2466.Count Ways To Build Good Strings/Solution2.ts diff --git a/solution/2400-2499/2466.Count Ways To Build Good Strings/README.md b/solution/2400-2499/2466.Count Ways To Build Good Strings/README.md index 88b8698471c7d..a4d50c1873929 100644 --- a/solution/2400-2499/2466.Count Ways To Build Good Strings/README.md +++ b/solution/2400-2499/2466.Count Ways To Build Good Strings/README.md @@ -196,4 +196,54 @@ func countGoodStrings(low int, high int, zero int, one int) int { <!-- solution:end --> +<!-- solution:start --> + +### Solution 2: Dynamic programming + +<!-- tabs:start --> + +#### TypeScript + +```ts +function countGoodStrings(low: number, high: number, zero: number, one: number): number { + const mod = 10 ** 9 + 7; + const f: number[] = new Array(high + 1).fill(0); + f[0] = 1; + + for (let i = 1; i <= high; i++) { + if (i >= zero) f[i] += f[i - zero]; + if (i >= one) f[i] += f[i - one]; + f[i] %= mod; + } + + const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0); + + return ans % mod; +} +``` + +#### JavaScript + +```js +function countGoodStrings(low, high, zero, one) { + const mod = 10 ** 9 + 7; + const f[] = new Array(high + 1).fill(0); + f[0] = 1; + + for (let i = 1; i <= high; i++) { + if (i >= zero) f[i] += f[i - zero]; + if (i >= one) f[i] += f[i - one]; + f[i] %= mod; + } + + const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0); + + return ans % mod; +} +``` + +<!-- tabs:end --> + +<!-- solution:end --> + <!-- problem:end --> diff --git a/solution/2400-2499/2466.Count Ways To Build Good Strings/README_EN.md b/solution/2400-2499/2466.Count Ways To Build Good Strings/README_EN.md index 00e065265953c..00eff483537a1 100644 --- a/solution/2400-2499/2466.Count Ways To Build Good Strings/README_EN.md +++ b/solution/2400-2499/2466.Count Ways To Build Good Strings/README_EN.md @@ -37,9 +37,9 @@ tags: <pre> <strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1 <strong>Output:</strong> 8 -<strong>Explanation:</strong> -One possible valid good string is "011". -It can be constructed as follows: "" -> "0" -> "01" -> "011". +<strong>Explanation:</strong> +One possible valid good string is "011". +It can be constructed as follows: "" -> "0" -> "01" -> "011". All binary strings from "000" to "111" are good strings in this example. </pre> @@ -196,4 +196,54 @@ func countGoodStrings(low int, high int, zero int, one int) int { <!-- solution:end --> +<!-- solution:start --> + +### Solution 2: Dynamic programming + +<!-- tabs:start --> + +#### TypeScript + +```ts +function countGoodStrings(low: number, high: number, zero: number, one: number): number { + const mod = 10 ** 9 + 7; + const f: number[] = new Array(high + 1).fill(0); + f[0] = 1; + + for (let i = 1; i <= high; i++) { + if (i >= zero) f[i] += f[i - zero]; + if (i >= one) f[i] += f[i - one]; + f[i] %= mod; + } + + const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0); + + return ans % mod; +} +``` + +#### JavaScript + +```js +function countGoodStrings(low, high, zero, one) { + const mod = 10 ** 9 + 7; + const f[] = new Array(high + 1).fill(0); + f[0] = 1; + + for (let i = 1; i <= high; i++) { + if (i >= zero) f[i] += f[i - zero]; + if (i >= one) f[i] += f[i - one]; + f[i] %= mod; + } + + const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0); + + return ans % mod; +} +``` + +<!-- tabs:end --> + +<!-- solution:end --> + <!-- problem:end --> diff --git a/solution/2400-2499/2466.Count Ways To Build Good Strings/Solution2.js b/solution/2400-2499/2466.Count Ways To Build Good Strings/Solution2.js new file mode 100644 index 0000000000000..6ef6105beca47 --- /dev/null +++ b/solution/2400-2499/2466.Count Ways To Build Good Strings/Solution2.js @@ -0,0 +1,15 @@ +function countGoodStrings(low, high, zero, one) { + const mod = 10 ** 9 + 7; + const f[] = new Array(high + 1).fill(0); + f[0] = 1; + + for (let i = 1; i <= high; i++) { + if (i >= zero) f[i] += f[i - zero]; + if (i >= one) f[i] += f[i - one]; + f[i] %= mod; + } + + const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0); + + return ans % mod; +} diff --git a/solution/2400-2499/2466.Count Ways To Build Good Strings/Solution2.ts b/solution/2400-2499/2466.Count Ways To Build Good Strings/Solution2.ts new file mode 100644 index 0000000000000..fea7b06e217eb --- /dev/null +++ b/solution/2400-2499/2466.Count Ways To Build Good Strings/Solution2.ts @@ -0,0 +1,15 @@ +function countGoodStrings(low: number, high: number, zero: number, one: number): number { + const mod = 10 ** 9 + 7; + const f: number[] = new Array(high + 1).fill(0); + f[0] = 1; + + for (let i = 1; i <= high; i++) { + if (i >= zero) f[i] += f[i - zero]; + if (i >= one) f[i] += f[i - one]; + f[i] %= mod; + } + + const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0); + + return ans % mod; +}