You are tasked with constructing stable binary arrays - a fascinating combinatorial problem that tests your dynamic programming skills!
Given three positive integers zero, one, and limit, you need to count how many different binary arrays can be formed that satisfy these conditions:
- The array contains exactly
zerooccurrences of 0 - The array contains exactly
oneoccurrences of 1 - No subarray of length greater than
limitcan contain only 0s or only 1s (this ensures stability)
The third condition is the key constraint - it prevents long streaks of consecutive identical digits, making the array "stable" by ensuring diversity in longer subsequences.
Return the total number of such stable binary arrays. Since this number can be astronomically large, return it modulo 109 + 7.
Example: If zero=1, one=1, limit=2, valid arrays are [0,1] and [1,0], so the answer is 2.
Input & Output
Visualization
Time & Space Complexity
We have zeroΓoneΓ2Γlimit possible states, each computed once
Memoization table stores results for all possible states
Constraints
- 1 β€ zero, one β€ 200
- 1 β€ limit β€ 200
- Answer must be returned modulo 109 + 7
- Array length will be zero + one