Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.2466 (#3908)
Browse files Browse the repository at this point in the history
  • Loading branch information
rain84 authored Dec 31, 2024
1 parent e7c5da6 commit 110b584
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 3 deletions.
57 changes: 57 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,61 @@ func countGoodStrings(low int, high int, zero int, one int) int {

<!-- solution:end -->

<!-- solution:start -->

### 方法二:动态规划

<!-- 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
/**
* @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 = 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,61 @@ 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
/**
* @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 = 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,22 @@
/**
* @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 = 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 110b584

Please sign in to comment.