Solving Leetcode Interviews in Seconds with AI: Find All Possible Stable Binary Arrays I
Introduction
In this blog post, we will explore how to solve the LeetCode problem "3129" 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], as both arrays have a single 0 and a single 1, and no subarray has a length greater than 2. Example 2: Input: zero = 1, one = 2, limit = 1 Output: 1 Explanation: The only possible stable binary array is [1,0,1]. Note that the binary arrays [1,1,0] and [0,1,1] have subarrays of length 2 with identical elements, hence, they are not stable. 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 <= 200
Explanation
Here's the solution:
- Dynamic Programming: Use a DP table
dp[i][j][k]whereiis the number of zeros placed,jis the number of ones placed, andkis the length of the trailing consecutive sequence of the same digit. - Base Case & Transitions: The base case is
dp[0][0][0] = 1. The transitions involve placing either a 0 or a 1, updating the count of zeros/ones and the trailing sequence length. A key optimization is resetting the trailing lengthkto 1 when placing a different digit. Limit Condition: When
kexceeds the limit, the state is invalid and contributes 0 to the result.Runtime Complexity: O(zero one limit)
- Storage Complexity: O(zero one limit)
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)]
dp[0][0][0] = 1
for i in range(zero + 1):
for j in range(one + 1):
for k in range(limit + 1):
if dp[i][j][k] == 0:
continue
# Place a zero
if i < zero:
if k < limit or k == 0:
if i == 0 and j == 0:
dp[i + 1][j][1] = (dp[i + 1][j][1] + dp[i][j][k]) % MOD
elif i > 0 and j > 0:
dp[i + 1][j][k + 1] = (dp[i + 1][j][k + 1] + dp[i][j][k]) % MOD
elif i == 0 and j == 0:
pass
# Place a one
if j < one:
if k < limit or k == 0:
if i == 0 and j == 0:
dp[i][j + 1][1] = (dp[i][j + 1][1] + dp[i][j][k]) % MOD
elif i > 0 and j > 0:
dp[i][j + 1][1] = (dp[i][j + 1][1] + dp[i][j][k]) % MOD
elif i == 0 and j == 0:
pass
ans = 0
for k in range(1, limit + 1):
ans = (ans + dp[zero][one][k]) % MOD
print(ans)
# Example Usage:
# zero = 1
# one = 1
# limit = 2
# The following lines simulate the given input
# Replace the following with input() if you want user input
input = lambda: next(input_generator)
input_str = "1\n1\n2"
input_generator = iter(input_str.splitlines())
solve()
input = lambda: next(input_generator)
input_str = "1\n2\n1"
input_generator = iter(input_str.splitlines())
solve()
input = lambda: next(input_generator)
input_str = "3\n3\n2"
input_generator = iter(input_str.splitlines())
solve()