Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.2466 #3908

Merged
merged 3 commits into from
Dec 31, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: add solutions to lc problem: No.2466
  • Loading branch information
rain84 authored and yanglbme committed Dec 31, 2024
commit bbd1f58bce64015253d8922c2435b92cf1cee248
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
@@ -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
@@ -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>

@@ -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;
}
Loading