Skip to content

Sri Hari: Batch-4/Neetcode-All/Added-articles #3773

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

Merged
merged 4 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
649 changes: 649 additions & 0 deletions articles/best-team-with-no-conflicts.md

Large diffs are not rendered by default.

283 changes: 283 additions & 0 deletions articles/count-ways-to-build-good-strings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
## 1. Recursion

::tabs-start

```python
class Solution:
def countGoodStrings(self, low: int, high: int, zero: int, one: int) -> int:
mod = 10**9 + 7

def dfs(length):
if length > high:
return 0
res = 1 if length >= low else 0
res += dfs(length + zero) + dfs(length + one)
return res % mod

return dfs(0)
```

```java
public class Solution {
final int mod = 1_000_000_007;

public int countGoodStrings(int low, int high, int zero, int one) {
return dfs(low, high, zero, one, 0);
}

private int dfs(int low, int high, int zero, int one, int length) {
if (length > high) return 0;
int res = (length >= low) ? 1 : 0;
res = (res + dfs(low, high, zero, one, length + zero)) % mod;
res = (res + dfs(low, high, zero, one, length + one)) % mod;
return res;
}
}
```

```cpp
class Solution {
public:
int countGoodStrings(int low, int high, int zero, int one) {
const int mod = 1e9 + 7;

function<int(int)> dfs = [&](int length) {
if (length > high) return 0;
int res = (length >= low) ? 1 : 0;
res = (res + dfs(length + zero)) % mod;
res = (res + dfs(length + one)) % mod;
return res;
};

return dfs(0);
}
};
```

```javascript
class Solution {
/**
* @param {number} low
* @param {number} high
* @param {number} zero
* @param {number} one
* @return {number}
*/
countGoodStrings(low, high, zero, one) {
const mod = 1e9 + 7;

const dfs = length => {
if (length > high) return 0;
let res = length >= low ? 1 : 0;
res = (res + dfs(length + zero)) % mod;
res = (res + dfs(length + one)) % mod;
return res;
};

return dfs(0);
}
}
```

::tabs-end

### Time & Space Complexity

* Time complexity: $O(2 ^ n)$
* Space complexity: $O(n)$

> Where $n$ is equal to the given $high$ value.

---

## 2. Dynamic Programming (Top-Down)

::tabs-start

```python
class Solution:
def countGoodStrings(self, low: int, high: int, zero: int, one: int) -> int:
mod = 10**9 + 7
dp = {}

def dfs(length):
if length > high:
return 0
if length in dp:
return dp[length]

dp[length] = 1 if length >= low else 0
dp[length] += dfs(length + zero) + dfs(length + one)
return dp[length] % mod

return dfs(0)
```

```java
public class Solution {
final int mod = 1_000_000_007;
private int[] dp;

public int countGoodStrings(int low, int high, int zero, int one) {
dp = new int[high + 1];
Arrays.fill(dp, -1);
return dfs(low, high, zero, one, 0);
}

private int dfs(int low, int high, int zero, int one, int length) {
if (length > high) return 0;
if (dp[length] != -1) return dp[length];

dp[length] = (length >= low) ? 1 : 0;
dp[length] = (dp[length] + dfs(low, high, zero, one, length + zero)) % mod;
dp[length] = (dp[length] + dfs(low, high, zero, one, length + one)) % mod;
return dp[length];
}
}
```

```cpp
class Solution {
const int mod = 1e9 + 7;
vector<int> dp;

public:
int countGoodStrings(int low, int high, int zero, int one) {
dp.assign(high + 1, -1);
return dfs(low, high, zero, one, 0);
}

private:
int dfs(int low, int high, int zero, int one, int length) {
if (length > high) return 0;
if (dp[length] != -1) return dp[length];
dp[length] = (length >= low) ? 1 : 0;
dp[length] = (dp[length] + dfs(low, high, zero, one, length + zero)) % mod;
dp[length] = (dp[length] + dfs(low, high, zero, one, length + one)) % mod;
return dp[length];
}
};
```

```javascript
class Solution {
/**
* @param {number} low
* @param {number} high
* @param {number} zero
* @param {number} one
* @return {number}
*/
countGoodStrings(low, high, zero, one) {
const mod = 1e9 + 7;
const dp = new Array(high + 1).fill(-1);

const dfs = length => {
if (length > high) return 0;
if (dp[length] !== -1) return dp[length];
dp[length] = length >= low ? 1 : 0;
dp[length] = (dp[length] + dfs(length + zero)) % mod;
dp[length] = (dp[length] + dfs(length + one)) % mod;
return dp[length];
};

return dfs(0);
}
}
```

::tabs-end

### Time & Space Complexity

* Time complexity: $O(n)$
* Space complexity: $O(n)$

> Where $n$ is equal to the given $high$ value.

---

## 3. Dynamic Programming (Bottom-Up)

::tabs-start

```python
class Solution:
def countGoodStrings(self, low: int, high: int, zero: int, one: int) -> int:
dp = { 0 : 1 }
mod = 10**9 + 7

for i in range(1, high + 1):
dp[i] = (dp.get(i - one, 0) + dp.get(i - zero, 0)) % mod

return sum(dp[i] for i in range(low, high + 1)) % mod
```

```java
public class Solution {
public int countGoodStrings(int low, int high, int zero, int one) {
int[] dp = new int[high + 1];
int mod = 1_000_000_007, res = 0;
dp[0] = 1;

for (int i = 1; i <= high; i++) {
if (i >= zero) dp[i] = (dp[i] + dp[i - zero]) % mod;
if (i >= one) dp[i] = (dp[i] + dp[i - one]) % mod;
if (i >= low) res = (res + dp[i]) % mod;
}
return res;
}
}
```

```cpp
class Solution {
public:
int countGoodStrings(int low, int high, int zero, int one) {
vector<int> dp(high + 1);
int mod = 1e9 + 7, res = 0;
dp[0] = 1;

for (int i = 1; i <= high; i++) {
if (i >= zero) dp[i] = (dp[i] + dp[i - zero]) % mod;
if (i >= one) dp[i] = (dp[i] + dp[i - one]) % mod;
if (i >= low) res = (res + dp[i]) % mod;
}
return res;
}
};
```

```javascript
class Solution {
/**
* @param {number} low
* @param {number} high
* @param {number} zero
* @param {number} one
* @return {number}
*/
countGoodStrings(low, high, zero, one) {
const mod = 1e9 + 7;
const dp = new Int32Array(high + 1);
let res = 0;
dp[0] = 1;

for (let i = 1; i <= high; i++) {
if (i >= zero) dp[i] = (dp[i] + dp[i - zero]) % mod;
if (i >= one) dp[i] = (dp[i] + dp[i - one]) % mod;
if (i >= low) res = (res + dp[i]) % mod;
}
return res;
}
}
```

::tabs-end

### Time & Space Complexity

* Time complexity: $O(n)$
* Space complexity: $O(n)$

> Where $n$ is equal to the given $high$ value.
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy