Solving Leetcode Interviews in Seconds with AI: Find All Possible Stable Binary Arrays II
Introduction
In this blog post, we will explore how to solve the LeetCode problem "3130" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
You are given 3 positive integers zero, one, and limit. A binary array arr is called stable if: The number of occurrences of 0 in arr is exactly zero. The number of occurrences of 1 in arr is exactly one. Each subarray of arr with a size greater than limit must contain both 0 and 1. Return the total number of stable binary arrays. Since the answer may be very large, return it modulo 109 + 7. Example 1: Input: zero = 1, one = 1, limit = 2 Output: 2 Explanation: The two possible stable binary arrays are [1,0] and [0,1]. Example 2: Input: zero = 1, one = 2, limit = 1 Output: 1 Explanation: The only possible stable binary array is [1,0,1]. Example 3: Input: zero = 3, one = 3, limit = 2 Output: 14 Explanation: All the possible stable binary arrays are [0,0,1,0,1,1], [0,0,1,1,0,1], [0,1,0,0,1,1], [0,1,0,1,0,1], [0,1,0,1,1,0], [0,1,1,0,0,1], [0,1,1,0,1,0], [1,0,0,1,0,1], [1,0,0,1,1,0], [1,0,1,0,0,1], [1,0,1,0,1,0], [1,0,1,1,0,0], [1,1,0,0,1,0], and [1,1,0,1,0,0]. Constraints: 1 <= zero, one, limit <= 1000
Explanation
Here's the breakdown of the problem and the Python code:
Approach:
- We use dynamic programming to store the number of ways to form stable arrays with a given number of zeros and ones placed, considering the limit constraint.
- The DP state
dp[i][j][k]represents the number of stable arrays formed withizeros andjones, wherekrepresents the number of consecutive zeros or ones at the end of the array. - We iterate through the possible number of zeros and ones and transition based on whether we can add a zero or a one while respecting the limit constraint.
Complexity:
- Runtime: O(zero * one * (limit + 1))
- Storage: O(zero * one * (limit + 1))
Code
def solve():
zero = int(input())
one = int(input())
limit = int(input())
MOD = 10**9 + 7
dp = [[[0] * (limit + 1) for _ in range(one + 1)] for _ in range(zero + 1)]
# Initialize base cases. If zero or one is 0. return 0
if zero == 0 or one == 0:
return 0
# Initialize the base cases with single '0' or single '1'. Since each must appear once.
dp[1][0][1] = 1 # array contains [0], zero =1, one =0
dp[0][1][1] = 1 # array contains [1], zero =0, one =1
for i in range(zero + 1):
for j in range(one + 1):
for k in range(1, limit + 1):
if i == 0 and j == 0:
continue
if i > 0 and k <= limit and dp[i-1][j][0] > 0: # Add a zero
dp[i][j][k] = (dp[i][j][k] + dp[i-1][j][0 if i == 1 else k -1 ]) % MOD # We can't add zero if array already contains zero
if j > 0 and k <= limit and dp[i][j-1][1] > 0: # Add a one
dp[i][j][k] = (dp[i][j][k] + dp[i][j-1][1 if j == 1 else k -1]) % MOD # We can't add one if array already contains one
#Add up zeros and ones consecutively up to the limit, and save that sum at index 0 and 1
if k < limit:
if i>0 and j>0:
dp[i][j][0] = (dp[i][j][0] + dp[i][j][k])% MOD
dp[i][j][1] = (dp[i][j][1] + dp[i][j][k])% MOD
result = 0
for k in range(1, limit + 1):
result = (result + dp[zero][one][k]) % MOD
return result
print(solve())