Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.2466
Browse files Browse the repository at this point in the history
  • Loading branch information
rain84 committed Dec 30, 2024
1 parent a336f65 commit 0e48212
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 3 deletions.
50 changes: 50 additions & 0 deletions solution/2400-2499/2466.Count Ways To Build Good Strings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->
Original file line number Diff line number Diff line change
Expand Up @@ -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 &quot;011&quot;.
It can be constructed as follows: &quot;&quot; -&gt; &quot;0&quot; -&gt; &quot;01&quot; -&gt; &quot;011&quot;.
<strong>Explanation:</strong>
One possible valid good string is &quot;011&quot;.
It can be constructed as follows: &quot;&quot; -&gt; &quot;0&quot; -&gt; &quot;01&quot; -&gt; &quot;011&quot;.
All binary strings from &quot;000&quot; to &quot;111&quot; are good strings in this example.
</pre>

Expand Down Expand Up @@ -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 -->
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 0e48212

Please sign in to comment.