给定两个 以0为索引 的二进制数组 nums1
和 nums2
。找出 最宽 的索引对 (i, j)
,使的 i <= j
并且 nums1[i] + nums1[i+1] + ... + nums1[j] == nums2[i] + nums2[i+1] + ... + nums2[j]
。
最宽 的指标对是指在 i
和 j
之间的 距离最大 的指标对。一对指标之间的 距离 定义为 j - i + 1
。
返回 最宽 索引对的 距离 。如果没有满足条件的索引对,则返回 0
。
示例 1:
输入: nums1 = [1,1,0,1], nums2 = [0,1,1,0] 输出: 3 解释: 如果i = 1, j = 3: Nums1 [1] + Nums1 [2] + Nums1[3] = 1 + 0 + 1 = 2。 Nums2 [1] + Nums2 [2] + Nums2[3] = 1 + 1 + 0 = 2。 i和j之间的距离是j - i + 1 = 3 - 1 + 1 = 3。
示例 2:
输入: nums1 = [0,1], nums2 = [1,1] 输出: 1 解释: If i = 1 and j = 1: nums1[1] = 1。 nums2[1] = 1。 i和j之间的距离是j - i + 1 = 1 - 1 + 1 = 1。
示例 3:
输入: nums1 = [0], nums2 = [1] 输出: 0 解释: 没有满足要求的索引对。
提示:
n == nums1.length == nums2.length
1 <= n <= 10^5
nums1[i]
仅为0
或1
.nums2[i]
仅为0
或1
.
提示 1
Keep prefix sums of both arrays.
提示 2
Can the difference between the prefix sums at an index help us?
提示 3
What happens if the difference between the two prefix sums at an index a is x, and x again at a different index b?
提示 4
This means that the sum of nums1 from index a + 1 to index b is equal to the sum of nums2 from index a + 1 to index b.
解法1:前缀和 + 哈希表
对于 i + 1<= j,
presum1[ i ] = sum( nums1[ 0...i ] ) , presum1[ j ] = sum ( nums1[ 0...j ] )
presum1[ j ] - presum1[ i ] = sum( nums1[ i + 1...j ] )
presum2[ i ] = sum( nums2[ 0...i ] ) , presum2[ j ] = sum ( nums2[ 0...j ] )
presum2[ j ] - presum2[ i ] = sum( nums2[ i + 1...j ] ), 索引对(i + 1, j) 宽度为 j - i
对于 i + 1 <= j , 枚举 j ,找到符合要求的 i ,因为要找最大宽度, 所以只需要在哈希表中记录该前缀和第一次出现的下标。
presum1[ j ] - presum1[ i ] == presum2[ j ] - presum2[ i ] 可以转换成
presum1[ j ] - presum2[ j ] == presum1[ i ] - presum2[ i ] ,
Java版:
class Solution {
public int widestPairOfIndices(int[] nums1, int[] nums2) {
long presum1 = 0;
long presum2 = 0;
int ans = 0;
Map<Long, Integer> map = new HashMap<>();
map.put(0L, -1);
for (int i = 0; i < nums1.length; i++) {
presum1 += nums1[i];
presum2 += nums2[i];
if (map.containsKey(presum1 - presum2)) {
ans = Math.max(ans, i - map.get(presum1 - presum2));
} else {
map.put(presum1 - presum2, i);
}
}
return ans;
}
}
Python3版:
class Solution:
def widestPairOfIndices(self, nums1: List[int], nums2: List[int]) -> int:
presum1 = 0
presum2 = 0
ans = 0
dic = {0: -1}
for i in range(len(nums1)):
presum1 += nums1[i]
presum2 += nums2[i]
if presum1 - presum2 in dic:
ans = max(ans, i - dic[presum1 - presum2])
else:
dic[presum1 - presum2] = i
return ans
复杂度分析
- 时间复杂度:O(n),其中 n 是数组 nums 1 和 nums 2 的长度。需要遍历两个数组一次计算差值前缀和,对于每个元素,更新前缀和、更新答案与操作哈希表的时间都是 O(1)。
- 空间复杂度:O(n),其中 n 是数组 nums 1 和 nums 2 的长度。哈希表的空间是 O(n)。
另一种写法:
Java版:
class Solution {
public int widestPairOfIndices(int[] nums1, int[] nums2) {
int presum = 0;
int ans = 0;
Map<Integer, Integer> map = new HashMap<>();
map.put(0, -1);
for (int i = 0; i < nums1.length; i++) {
presum += nums1[i] - nums2[i];
if (map.containsKey(presum)) {
ans = Math.max(ans, i - map.get(presum));
} else {
map.put(presum, i);
}
}
return ans;
}
}
Python3版:
class Solution:
def widestPairOfIndices(self, nums1: List[int], nums2: List[int]) -> int:
presum = 0
ans = 0
dic = {0: -1}
for i in range(len(nums1)):
presum += nums1[i] - nums2[i]
if presum in dic:
ans = max(ans, i - dic[presum])
else:
dic[presum] = i
return ans