Skip to content

Commit 1bad23c

Browse files
chore: add LeetCode daily solution
1 parent 619ffff commit 1bad23c

5 files changed

Lines changed: 113 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Find All Possible Stable Binary Arrays II (Hard)
2+
3+
**Problem ID:** 3130
4+
**Date:** 2026-03-10
5+
**Link:** https://leetcode.com/problems/find-all-possible-stable-binary-arrays-ii/
6+
7+
## Approach
8+
9+
To solve the problem of finding all possible stable binary arrays given the constraints on the number of zeros, ones, and the limit on subarray sizes, we can utilize a dynamic programming (DP) approach.
10+
11+
### Main Idea:
12+
The core idea is to build valid binary arrays by considering the placement of zeros and ones while adhering to the stability condition. A stable binary array must have exactly one '1', and any subarray longer than the specified limit must include both '0' and '1'.
13+
14+
### Dynamic Programming Approach:
15+
1. **State Definition**:
16+
Define a DP table `dp[i][j]` where `i` represents the number of zeros used and `j` represents the number of ones used. The value `dp[i][j]` will store the number of valid stable binary arrays that can be formed with `i` zeros and `j` ones.
17+
18+
2. **Initialization**:
19+
Initialize `dp[0][1]` to 1, as the only stable array with no zeros and one '1' is `[1]`.
20+
21+
3. **Transition**:
22+
For each combination of zeros `i` and ones `j`, we can consider two cases:
23+
- **Adding a '1'**: If we add a '1', we can only do this if we have not exceeded the limit on the number of zeros that can precede it. This means we can add a '1' after a certain number of zeros.
24+
- **Adding a '0'**: When adding a '0', we need to ensure that it does not violate the stability condition. Specifically, if we add a '0', we must ensure that the last segment of the array (of size greater than the limit) contains at least one '1'.
25+
26+
The transition can be calculated based on previously computed states, ensuring that we respect the limit condition.
27+
28+
4. **Final Calculation**:
29+
The result will be the sum of all valid configurations stored in `dp[zero][1]` for varying numbers of zeros, ensuring that we only consider configurations that include the required number of zeros and exactly one '1'.
30+
31+
### Data Structures:
32+
- A 2D list (or array) `dp` to store the number of valid configurations for different counts of zeros and ones.
33+
34+
### Complexity:
35+
The time complexity of this approach is O(zero * one), where `zero` and `one` are the counts of zeros and ones respectively. This is due to the nested loops iterating through possible counts of zeros and ones. The space complexity is also O(zero * one) for storing the DP table.
36+
37+
### Conclusion:
38+
By leveraging dynamic programming, we can efficiently count the number of valid stable binary arrays while adhering to the constraints provided. This method ensures that we explore all configurations systematically and avoids redundant calculations through memoization.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
private static final int MOD = 1000000007;
3+
4+
public int countStableArrays(int zero, int one, int limit) {
5+
long[][] dp = new long[zero + 1][one + 1];
6+
dp[0][1] = 1; // Base case: one way to have one '1' and no '0's
7+
8+
for (int z = 0; z <= zero; z++) {
9+
for (int o = 0; o <= one; o++) {
10+
if (z > 0) {
11+
dp[z][o] = (dp[z][o] + dp[z - 1][o]) % MOD; // Adding a '0'
12+
}
13+
if (o > 0) {
14+
dp[z][o] = (dp[z][o] + dp[z][o - 1]) % MOD; // Adding a '1'
15+
}
16+
if (z > 0 && o > 0 && z + o > limit) {
17+
dp[z][o] = (dp[z][o] - dp[Math.max(0, z - limit)][o] + MOD) % MOD; // Removing invalid cases
18+
}
19+
}
20+
}
21+
22+
return (int) dp[zero][one];
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const MOD = 1e9 + 7;
2+
3+
var countStableArrays = function(zero, one, limit) {
4+
const dp = Array.from({ length: zero + 1 }, () => Array(one + 1).fill(0));
5+
dp[0][1] = 1; // Base case: one way to have one '1' and no '0's
6+
7+
for (let z = 0; z <= zero; z++) {
8+
for (let o = 0; o <= one; o++) {
9+
if (z > 0) {
10+
dp[z][o] = (dp[z][o] + dp[z - 1][o]) % MOD; // Add a '0'
11+
}
12+
if (o > 1) {
13+
dp[z][o] = (dp[z][o] + dp[z][o - 1]) % MOD; // Add a '1'
14+
}
15+
if (z > 0 && o > 0 && z + o > limit) {
16+
dp[z][o] = (dp[z][o] - dp[z - 1][o - 1] + MOD) % MOD; // Remove invalid cases
17+
}
18+
}
19+
}
20+
21+
let result = 0;
22+
for (let z = 0; z <= zero; z++) {
23+
result = (result + dp[z][one]) % MOD;
24+
}
25+
26+
return result;
27+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def countStableArrays(self, zero: int, one: int, limit: int) -> int:
3+
MOD = 10**9 + 7
4+
5+
dp = [[0] * (one + 1) for _ in range(zero + 1)]
6+
dp[0][1] = 1 # Base case: one way to have a single 1
7+
8+
for z in range(zero + 1):
9+
for o in range(1, one + 1):
10+
if z > 0:
11+
dp[z][o] = (dp[z][o] + dp[z - 1][o]) % MOD
12+
if o > 1:
13+
dp[z][o] = (dp[z][o] + dp[z][o - 1]) % MOD
14+
if z > 0 and o > 1 and z + o > limit:
15+
dp[z][o] = (dp[z][o] - dp[z - 1][o - 1] + MOD) % MOD
16+
17+
return dp[zero][one]
18+
19+
# Example usage:
20+
# sol = Solution()
21+
# print(sol.countStableArrays(1, 1, 2)) # Output: 2
22+
# print(sol.countStableArrays(1, 2, 1)) # Output: 1
23+
# print(sol.countStableArrays(3, 3, 2)) # Output: 14

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,3 +388,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
388388
- 2026-03-07 — [Minimum Number of Flips to Make the Binary String Alternating](https://leetcode.com/problems/minimum-number-of-flips-to-make-the-binary-string-alternating/) (Medium) → `Medium/2026-03-07-1888-Minimum-Number-of-Flips-to-Make-the-Binary-String-Alternating`
389389
- 2026-03-08 — [Find Unique Binary String](https://leetcode.com/problems/find-unique-binary-string/) (Medium) → `Medium/2026-03-08-1980-Find-Unique-Binary-String`
390390
- 2026-03-09 — [Find All Possible Stable Binary Arrays I](https://leetcode.com/problems/find-all-possible-stable-binary-arrays-i/) (Medium) → `Medium/2026-03-09-3129-Find-All-Possible-Stable-Binary-Arrays-I`
391+
- 2026-03-10 — [Find All Possible Stable Binary Arrays II](https://leetcode.com/problems/find-all-possible-stable-binary-arrays-ii/) (Hard) → `Hard/2026-03-10-3130-Find-All-Possible-Stable-Binary-Arrays-II`

0 commit comments

Comments
 (0)