From bbd1f58bce64015253d8922c2435b92cf1cee248 Mon Sep 17 00:00:00 2001 From: rain84 Date: Tue, 31 Dec 2024 00:55:37 +0300 Subject: [PATCH 1/3] 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 2: Dynamic programming + + + +#### 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; +} +``` + + + + + 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:
 Input: low = 3, high = 3, zero = 1, one = 1
 Output: 8
-Explanation: 
-One possible valid good string is "011". 
-It can be constructed as follows: "" -> "0" -> "01" -> "011". 
+Explanation:
+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.
 
@@ -196,4 +196,54 @@ func countGoodStrings(low int, high int, zero int, one int) int { + + +### Solution 2: Dynamic programming + + + +#### 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; +} +``` + + + + + 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; +} From 96480bc35d69dadc5012f8ff716b7a332ca3a719 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 31 Dec 2024 09:49:35 +0800 Subject: [PATCH 2/3] fix: js code --- .../2466.Count Ways To Build Good Strings/README.md | 9 ++++++++- .../2466.Count Ways To Build Good Strings/README_EN.md | 9 ++++++++- .../2466.Count Ways To Build Good Strings/Solution2.js | 9 ++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) 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 a4d50c1873929..92f9bb33c25c9 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 @@ -225,9 +225,16 @@ function countGoodStrings(low: number, high: number, zero: number, one: number): #### JavaScript ```js +/** + * @param {number} low + * @param {number} high + * @param {number} zero + * @param {number} one + * @return {number} + */ function countGoodStrings(low, high, zero, one) { const mod = 10 ** 9 + 7; - const f[] = new Array(high + 1).fill(0); + const f = Array(high + 1).fill(0); f[0] = 1; for (let i = 1; i <= high; i++) { 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 00eff483537a1..80a98810d2a79 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 @@ -225,9 +225,16 @@ function countGoodStrings(low: number, high: number, zero: number, one: number): #### JavaScript ```js +/** + * @param {number} low + * @param {number} high + * @param {number} zero + * @param {number} one + * @return {number} + */ function countGoodStrings(low, high, zero, one) { const mod = 10 ** 9 + 7; - const f[] = new Array(high + 1).fill(0); + const f = Array(high + 1).fill(0); f[0] = 1; for (let i = 1; i <= high; i++) { 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 index 6ef6105beca47..d58e19aaa87b4 100644 --- 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 @@ -1,6 +1,13 @@ +/** + * @param {number} low + * @param {number} high + * @param {number} zero + * @param {number} one + * @return {number} + */ function countGoodStrings(low, high, zero, one) { const mod = 10 ** 9 + 7; - const f[] = new Array(high + 1).fill(0); + const f = Array(high + 1).fill(0); f[0] = 1; for (let i = 1; i <= high; i++) { From aedaa91c7fcd41505e59bf3e23635b4f0f9b7eaa Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 31 Dec 2024 09:50:28 +0800 Subject: [PATCH 3/3] docs: translate section title to Chinese --- .../2400-2499/2466.Count Ways To Build Good Strings/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 92f9bb33c25c9..4f24a4bea1c79 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 @@ -198,7 +198,7 @@ func countGoodStrings(low int, high int, zero int, one int) int { -### Solution 2: Dynamic programming +### 方法二:动态规划