Choose Numbers From Two Arrays in Range Problem
Description
LeetCode Problem 2143.
You are given two 0-indexed integer arrays nums1 and nums2 of length n.
A range [l, r] (inclusive) where 0 <= l <= r < n is balanced if:
- For every
iin the range[l, r], you pick eithernums1[i]ornums2[i]. - The sum of the numbers you pick from
nums1equals to the sum of the numbers you pick fromnums2(the sum is considered to be0if you pick no numbers from an array).
Two balanced ranges from [l_1, r_1] and [l_2, r_2] are considered to be different if at least one of the following is true:
l_1 != l_2r_1 != r_2nums1[i]is picked in the first range, andnums2[i]is picked in the second range or vice versa for at least onei.
Return the number of different ranges that are balanced. Since the answer may be very large, return it modulo 10^9 + 7.
Example 1:
Input: nums1 = [1,2,5], nums2 = [2,6,3]
Output: 3
Explanation: The balanced ranges are:
- [0, 1] where we choose nums2[0], and nums1[1].
The sum of the numbers chosen from nums1 equals the sum of the numbers chosen from nums2: 2 = 2.
- [0, 2] where we choose nums1[0], nums2[1], and nums1[2].
The sum of the numbers chosen from nums1 equals the sum of the numbers chosen from nums2: 1 + 5 = 6.
- [0, 2] where we choose nums1[0], nums1[1], and nums2[2].
The sum of the numbers chosen from nums1 equals the sum of the numbers chosen from nums2: 1 + 2 = 3.
Note that the second and third balanced ranges are different.
In the second balanced range, we choose nums2[1] and in the third balanced range, we choose nums1[1].
Example 2:
Input: nums1 = [0,1], nums2 = [1,0]
Output: 4
Explanation: The balanced ranges are:
- [0, 0] where we choose nums1[0].
The sum of the numbers chosen from nums1 equals the sum of the numbers chosen from nums2: 0 = 0.
- [1, 1] where we choose nums2[1].
The sum of the numbers chosen from nums1 equals the sum of the numbers chosen from nums2: 0 = 0.
- [0, 1] where we choose nums1[0] and nums2[1].
The sum of the numbers chosen from nums1 equals the sum of the numbers chosen from nums2: 0 = 0.
- [0, 1] where we choose nums2[0] and nums1[1].
The sum of the numbers chosen from nums1 equals the sum of the numbers chosen from nums2: 1 = 1.
Constraints:
n == nums1.length == nums2.length1 <= n <= 1000 <= nums1[i], nums2[i] <= 100
Solution
Approach: Dynamic Programming
For each index in a subrange we pick nums1[i] or nums2[i]. Count the picks as a signed sum: +nums1[i] for the first array and -nums2[i] for the second. The sums of the two groups are equal exactly when this signed total is 0.
Let f[i][j] be the number of ways for subranges ending at i whose signed total is j - s2, where s1 and s2 are the sums of the two arrays (the offset keeps indices non-negative). At each i, start a new subrange (one way each for +a and -b) and extend the ways ending at i - 1. Add f[i][s2] (total 0) to the answer, modulo 10^9 + 7.
Time O(n (s1 + s2)), space the same.
class Solution:
def countSubranges(self, nums1: List[int], nums2: List[int]) -> int:
n = len(nums1)
s1, s2 = sum(nums1), sum(nums2)
f = [[0] * (s1 + s2 + 1) for _ in range(n)]
ans = 0
mod = 10**9 + 7
for i, (a, b) in enumerate(zip(nums1, nums2)):
f[i][a + s2] += 1
f[i][-b + s2] += 1
if i:
for j in range(s1 + s2 + 1):
if j >= a:
f[i][j] = (f[i][j] + f[i - 1][j - a]) % mod
if j + b < s1 + s2 + 1:
f[i][j] = (f[i][j] + f[i - 1][j + b]) % mod
ans = (ans + f[i][s2]) % mod
return ans