|
| 1 | +# [Problem 3129: Find All Possible Stable Binary Arrays I](https://leetcode.com/problems/find-all-possible-stable-binary-arrays-i/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +The condition that every subarray of size > limit must contain both 0 and 1 means that no run (consecutive identical elements) may have length > limit. So we just need to count binary strings of length n = zero + one with exactly `zero` zeros and `one` ones, and with every run length between 1 and `limit` inclusive. |
| 5 | + |
| 6 | +We can think of the string as alternating blocks of zeros and ones. Let a = number of zero-blocks, b = number of one-blocks. Each block length is at least 1 and at most `limit`. The zeros must sum to `zero`, the ones to `one`. So the number of ways is the number of compositions of `zero` into `a` parts each in [1, limit], times the number of compositions of `one` into `b` parts in [1, limit]. Which (a,b) pairs are possible? For alternating blocks, |a-b| <= 1. Also if a == b there are two possible starting bits (start with 0 or start with 1), otherwise the larger count's bit must be the starting bit. So we can iterate all a,b with 1 <= a <= zero, 1 <= b <= one, |a-b| <= 1, compute the two composition counts and add up (times 2 if a==b else times 1). |
| 7 | + |
| 8 | +Counting compositions with upper bound (each part between 1 and limit) can be done with stars-and-bars + inclusion-exclusion: |
| 9 | +number of positive integer solutions x1+...+k = N with 1 <= xi <= L equals |
| 10 | +sum_{j=0}^{k} (-1)^j * C(k, j) * C(N - j*L - 1, k-1) |
| 11 | +where C(n,k)=0 if n<k or n<0. |
| 12 | + |
| 13 | +We can precompute factorials/inv factorials for combinations modulo 1e9+7. Memoize composition counts for (N,k) to avoid recomputation. |
| 14 | + |
| 15 | +Constraints are small (<=200), so this approach is efficient. |
| 16 | + |
| 17 | +## Refining the problem, round 2 thoughts |
| 18 | +- Edge cases: k > N (cannot split N into more than N positive parts) -> zero ways. Also k==0 only if N==0, but inputs guarantee zero,one >=1 so k>=1 here. |
| 19 | +- Inclusion-exclusion needs careful bounds; we can cap j by floor((N-k)/L) or simply check the comb arguments. |
| 20 | +- Complexity: iterating a in [1..zero] and b in [1..one] but only pairs with |a-b|<=1 -> about O(min(zero,one)*2) pairs, each requiring two composition counts (but we'll memoize so each (N,k) computed once). Composition computation itself loops j up to ~ (N-k)/L which is <= N. So overall complexity roughly O((zero+one) * N) with small constants; safe for ≤200. |
| 21 | +- Implementation details: precompute factorials up to zero+one (<=400) for combinations, modular inverse via pow. |
| 22 | + |
| 23 | +## Attempted solution(s) |
| 24 | +```python |
| 25 | +MOD = 10**9 + 7 |
| 26 | + |
| 27 | +class Solution: |
| 28 | + def countStableArrays(self, zero: int, one: int, limit: int) -> int: |
| 29 | + # Precompute factorials up to maxN |
| 30 | + maxN = zero + one + 5 |
| 31 | + fact = [1] * (maxN) |
| 32 | + invfact = [1] * (maxN) |
| 33 | + for i in range(1, maxN): |
| 34 | + fact[i] = fact[i-1] * i % MOD |
| 35 | + invfact[-1] = pow(fact[-1], MOD-2, MOD) |
| 36 | + for i in range(maxN-2, -1, -1): |
| 37 | + invfact[i] = invfact[i+1] * (i+1) % MOD |
| 38 | + |
| 39 | + def comb(n, k): |
| 40 | + if n < 0 or k < 0 or k > n: |
| 41 | + return 0 |
| 42 | + return fact[n] * invfact[k] % MOD * invfact[n-k] % MOD |
| 43 | + |
| 44 | + # number of ways to write N as sum of k positive integers each <= L |
| 45 | + from functools import lru_cache |
| 46 | + @lru_cache(None) |
| 47 | + def compositions_bounded(N, k, L): |
| 48 | + if k == 0: |
| 49 | + return 1 if N == 0 else 0 |
| 50 | + if k > N: |
| 51 | + return 0 |
| 52 | + # transform to nonnegative: yi = xi - 1, sum yi = N - k, each yi <= L-1 |
| 53 | + S = N - k |
| 54 | + U = L - 1 |
| 55 | + # inclusion-exclusion: |
| 56 | + res = 0 |
| 57 | + # j up to min(k, floor(S/(U+1))) where U+1 = L |
| 58 | + maxj = S // L |
| 59 | + for j in range(0, min(k, maxj) + 1): |
| 60 | + sign = -1 if (j % 2 == 1) else 1 |
| 61 | + term = comb(k, j) * comb(N - j*L - 1, k-1) % MOD |
| 62 | + if sign == 1: |
| 63 | + res = (res + term) % MOD |
| 64 | + else: |
| 65 | + res = (res - term) % MOD |
| 66 | + return res % MOD |
| 67 | + |
| 68 | + ans = 0 |
| 69 | + # iterate number of zero-blocks a and one-blocks b |
| 70 | + for a in range(1, zero+1): |
| 71 | + for b in (a-1, a, a+1): |
| 72 | + if b < 1 or b > one: |
| 73 | + continue |
| 74 | + if abs(a-b) > 1: |
| 75 | + continue |
| 76 | + ways0 = compositions_bounded(zero, a, limit) |
| 77 | + if ways0 == 0: |
| 78 | + continue |
| 79 | + ways1 = compositions_bounded(one, b, limit) |
| 80 | + if ways1 == 0: |
| 81 | + continue |
| 82 | + multiplicity = 2 if a == b else 1 |
| 83 | + ans = (ans + ways0 * ways1 % MOD * multiplicity) % MOD |
| 84 | + |
| 85 | + return ans |
| 86 | + |
| 87 | +# For LeetCode function name compatibility: |
| 88 | +def countStableArrays(zero: int, one: int, limit: int) -> int: |
| 89 | + return Solution().countStableArrays(zero, one, limit) |
| 90 | +``` |
| 91 | + |
| 92 | +- Notes about the approach: |
| 93 | + - We model the array as alternating blocks of zeros and ones. Let a be the count of zero-blocks and b the count of one-blocks. Each block length is in [1, limit]. The counts of blocks satisfy |a-b| <= 1. When a == b we have two possible starting bits; otherwise exactly one starting bit is possible. |
| 94 | + - For each (a,b) pair we multiply the number of bounded compositions of `zero` into `a` parts and `one` into `b` parts, and add to the result (times 2 if a == b). |
| 95 | + - The bounded composition count uses inclusion-exclusion on transformed nonnegative solutions. |
| 96 | +- Complexity: |
| 97 | + - Time: roughly O((zero+one) * N_max) where N_max <= 400 in practice; more concretely, nested loops over a (<=zero) and b (near a), and for each distinct (N,k) we do inclusion-exclusion up to O(N/L) terms. This is easily within limits for zero,one <= 200. |
| 98 | + - Space: O(N_max) for factorials and O(number of cached (N,k)) for memoization (<= O(zero+one)^2 worst case but practically small). |
0 commit comments