Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 110b584

Browse files
authored
feat: add solutions to lc problem: No.2466 (#3908)
1 parent e7c5da6 commit 110b584

File tree

4 files changed

+154
-3
lines changed

4 files changed

+154
-3
lines changed

‎solution/2400-2499/2466.Count Ways To Build Good Strings/README.md‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,61 @@ func countGoodStrings(low int, high int, zero int, one int) int {
196196

197197
<!-- solution:end -->
198198

199+
<!-- solution:start -->
200+
201+
### 方法二:动态规划
202+
203+
<!-- tabs:start -->
204+
205+
#### TypeScript
206+
207+
```ts
208+
function countGoodStrings(low: number, high: number, zero: number, one: number): number {
209+
const mod = 10 ** 9 + 7;
210+
const f: number[] = new Array(high + 1).fill(0);
211+
f[0] = 1;
212+
213+
for (let i = 1; i <= high; i++) {
214+
if (i >= zero) f[i] += f[i - zero];
215+
if (i >= one) f[i] += f[i - one];
216+
f[i] %= mod;
217+
}
218+
219+
const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0);
220+
221+
return ans % mod;
222+
}
223+
```
224+
225+
#### JavaScript
226+
227+
```js
228+
/**
229+
* @param {number} low
230+
* @param {number} high
231+
* @param {number} zero
232+
* @param {number} one
233+
* @return {number}
234+
*/
235+
function countGoodStrings(low, high, zero, one) {
236+
const mod = 10 ** 9 + 7;
237+
const f = Array(high + 1).fill(0);
238+
f[0] = 1;
239+
240+
for (let i = 1; i <= high; i++) {
241+
if (i >= zero) f[i] += f[i - zero];
242+
if (i >= one) f[i] += f[i - one];
243+
f[i] %= mod;
244+
}
245+
246+
const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0);
247+
248+
return ans % mod;
249+
}
250+
```
251+
252+
<!-- tabs:end -->
253+
254+
<!-- solution:end -->
255+
199256
<!-- problem:end -->

‎solution/2400-2499/2466.Count Ways To Build Good Strings/README_EN.md‎

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ tags:
3737
<pre>
3838
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
3939
<strong>Output:</strong> 8
40-
<strong>Explanation:</strong>
41-
One possible valid good string is &quot;011&quot;.
42-
It can be constructed as follows: &quot;&quot; -&gt; &quot;0&quot; -&gt; &quot;01&quot; -&gt; &quot;011&quot;.
40+
<strong>Explanation:</strong>
41+
One possible valid good string is &quot;011&quot;.
42+
It can be constructed as follows: &quot;&quot; -&gt; &quot;0&quot; -&gt; &quot;01&quot; -&gt; &quot;011&quot;.
4343
All binary strings from &quot;000&quot; to &quot;111&quot; are good strings in this example.
4444
</pre>
4545

@@ -196,4 +196,61 @@ func countGoodStrings(low int, high int, zero int, one int) int {
196196

197197
<!-- solution:end -->
198198

199+
<!-- solution:start -->
200+
201+
### Solution 2: Dynamic programming
202+
203+
<!-- tabs:start -->
204+
205+
#### TypeScript
206+
207+
```ts
208+
function countGoodStrings(low: number, high: number, zero: number, one: number): number {
209+
const mod = 10 ** 9 + 7;
210+
const f: number[] = new Array(high + 1).fill(0);
211+
f[0] = 1;
212+
213+
for (let i = 1; i <= high; i++) {
214+
if (i >= zero) f[i] += f[i - zero];
215+
if (i >= one) f[i] += f[i - one];
216+
f[i] %= mod;
217+
}
218+
219+
const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0);
220+
221+
return ans % mod;
222+
}
223+
```
224+
225+
#### JavaScript
226+
227+
```js
228+
/**
229+
* @param {number} low
230+
* @param {number} high
231+
* @param {number} zero
232+
* @param {number} one
233+
* @return {number}
234+
*/
235+
function countGoodStrings(low, high, zero, one) {
236+
const mod = 10 ** 9 + 7;
237+
const f = Array(high + 1).fill(0);
238+
f[0] = 1;
239+
240+
for (let i = 1; i <= high; i++) {
241+
if (i >= zero) f[i] += f[i - zero];
242+
if (i >= one) f[i] += f[i - one];
243+
f[i] %= mod;
244+
}
245+
246+
const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0);
247+
248+
return ans % mod;
249+
}
250+
```
251+
252+
<!-- tabs:end -->
253+
254+
<!-- solution:end -->
255+
199256
<!-- problem:end -->
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number} low
3+
* @param {number} high
4+
* @param {number} zero
5+
* @param {number} one
6+
* @return {number}
7+
*/
8+
function countGoodStrings(low, high, zero, one) {
9+
const mod = 10 ** 9 + 7;
10+
const f = Array(high + 1).fill(0);
11+
f[0] = 1;
12+
13+
for (let i = 1; i <= high; i++) {
14+
if (i >= zero) f[i] += f[i - zero];
15+
if (i >= one) f[i] += f[i - one];
16+
f[i] %= mod;
17+
}
18+
19+
const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0);
20+
21+
return ans % mod;
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function countGoodStrings(low: number, high: number, zero: number, one: number): number {
2+
const mod = 10 ** 9 + 7;
3+
const f: number[] = new Array(high + 1).fill(0);
4+
f[0] = 1;
5+
6+
for (let i = 1; i <= high; i++) {
7+
if (i >= zero) f[i] += f[i - zero];
8+
if (i >= one) f[i] += f[i - one];
9+
f[i] %= mod;
10+
}
11+
12+
const ans = f.slice(low, high + 1).reduce((acc, cur) => acc + cur, 0);
13+
14+
return ans % mod;
15+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /