Skip to content

Commit 9a8dc81

Browse files
committed
feat: add solutions to lc problem: No.1983
No.1983.Widest Pair of Indices With Equal Range Sum
1 parent d81e053 commit 9a8dc81

File tree

6 files changed

+231
-2
lines changed

6 files changed

+231
-2
lines changed

solution/1900-1999/1983.Widest Pair of Indices With Equal Range Sum/README.md

+88-1
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,109 @@ i和j之间的距离是j - i + 1 = 1 - 1 + 1 = 1。
6262

6363
<!-- 这里可写通用的实现逻辑 -->
6464

65+
**方法一:前缀和 + 哈希表**
66+
67+
我们观察到,对于任意的索引对 $(i, j)$,如果 $nums1[i] + nums1[i+1] + ... + nums1[j] = nums2[i] + nums2[i+1] + ... + nums2[j]$,那么 $nums1[i] - nums2[i] + nums1[i+1] - nums2[i+1] + ... + nums1[j] - nums2[j] = 0$。如果我们将数组 $nums1$ 与数组 $nums2$ 对应位置的元素相减,得到一个新的数组 $nums$,那么问题转换为在数组 $nums$ 中找到一个最长的子数组,使得子数组的和为 $0$。这可以通过前缀和 + 哈希表的方法求解。
68+
69+
我们定义一个变量 $s$ 表示当前 $nums$ 的前缀和,用一个哈希表 $d$ 保存每个前缀和第一次出现的位置。初始时 $s = 0$, $d[0] = -1$。
70+
71+
接下来,我们遍历数组 $nums$ 中的每个元素 $x$,计算 $s$ 的值,然后检查哈希表中是否存在 $s$,如果哈希表存在 $s$,那么说明存在一个子数组 $nums[d[s]+1,..i]$,使得子数组的和为 $0$,我们更新答案为 $max(ans, i - d[s])$。否则,我们将 $s$ 的值加入哈希表中,表示 $s$ 第一次出现的位置为 $i$。
72+
73+
遍历结束,即可得到最终的答案。
74+
75+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
76+
6577
<!-- tabs:start -->
6678

6779
### **Python3**
6880

6981
<!-- 这里可写当前语言的特殊实现逻辑 -->
7082

7183
```python
72-
84+
class Solution:
85+
def widestPairOfIndices(self, nums1: List[int], nums2: List[int]) -> int:
86+
d = {0: -1}
87+
ans = s = 0
88+
for i, (a, b) in enumerate(zip(nums1, nums2)):
89+
s += a - b
90+
if s in d:
91+
ans = max(ans, i - d[s])
92+
else:
93+
d[s] = i
94+
return ans
7395
```
7496

7597
### **Java**
7698

7799
<!-- 这里可写当前语言的特殊实现逻辑 -->
78100

79101
```java
102+
class Solution {
103+
public int widestPairOfIndices(int[] nums1, int[] nums2) {
104+
Map<Integer, Integer> d = new HashMap<>();
105+
d.put(0, -1);
106+
int n = nums1.length;
107+
int s = 0;
108+
int ans = 0;
109+
for (int i = 0; i < n; ++i) {
110+
s += nums1[i] - nums2[i];
111+
if (d.containsKey(s)) {
112+
ans = Math.max(ans, i - d.get(s));
113+
} else {
114+
d.put(s, i);
115+
}
116+
}
117+
return ans;
118+
}
119+
}
120+
```
121+
122+
### **C++**
123+
124+
```cpp
125+
class Solution {
126+
public:
127+
int widestPairOfIndices(vector<int>& nums1, vector<int>& nums2) {
128+
unordered_map<int, int> d;
129+
d[0] = -1;
130+
int ans = 0, s = 0;
131+
int n = nums1.size();
132+
for (int i = 0; i < n; ++i) {
133+
s += nums1[i] - nums2[i];
134+
if (d.count(s)) {
135+
ans = max(ans, i - d[s]);
136+
} else {
137+
d[s] = i;
138+
}
139+
}
140+
return ans;
141+
}
142+
};
143+
```
80144
145+
### **Go**
146+
147+
```go
148+
func widestPairOfIndices(nums1 []int, nums2 []int) (ans int) {
149+
d := map[int]int{0: -1}
150+
s := 0
151+
for i := range nums1 {
152+
s += nums1[i] - nums2[i]
153+
if j, ok := d[s]; ok {
154+
ans = max(ans, i-j)
155+
} else {
156+
d[s] = i
157+
}
158+
}
159+
return
160+
}
161+
162+
func max(a, b int) int {
163+
if a > b {
164+
return a
165+
}
166+
return b
167+
}
81168
```
82169

83170
### **...**

solution/1900-1999/1983.Widest Pair of Indices With Equal Range Sum/README_EN.md

+76-1
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,88 @@ There are no pairs of indices that meet the requirements.
6161
### **Python3**
6262

6363
```python
64-
64+
class Solution:
65+
def widestPairOfIndices(self, nums1: List[int], nums2: List[int]) -> int:
66+
d = {0: -1}
67+
ans = s = 0
68+
for i, (a, b) in enumerate(zip(nums1, nums2)):
69+
s += a - b
70+
if s in d:
71+
ans = max(ans, i - d[s])
72+
else:
73+
d[s] = i
74+
return ans
6575
```
6676

6777
### **Java**
6878

6979
```java
80+
class Solution {
81+
public int widestPairOfIndices(int[] nums1, int[] nums2) {
82+
Map<Integer, Integer> d = new HashMap<>();
83+
d.put(0, -1);
84+
int n = nums1.length;
85+
int s = 0;
86+
int ans = 0;
87+
for (int i = 0; i < n; ++i) {
88+
s += nums1[i] - nums2[i];
89+
if (d.containsKey(s)) {
90+
ans = Math.max(ans, i - d.get(s));
91+
} else {
92+
d.put(s, i);
93+
}
94+
}
95+
return ans;
96+
}
97+
}
98+
```
99+
100+
### **C++**
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
int widestPairOfIndices(vector<int>& nums1, vector<int>& nums2) {
106+
unordered_map<int, int> d;
107+
d[0] = -1;
108+
int ans = 0, s = 0;
109+
int n = nums1.size();
110+
for (int i = 0; i < n; ++i) {
111+
s += nums1[i] - nums2[i];
112+
if (d.count(s)) {
113+
ans = max(ans, i - d[s]);
114+
} else {
115+
d[s] = i;
116+
}
117+
}
118+
return ans;
119+
}
120+
};
121+
```
70122
123+
### **Go**
124+
125+
```go
126+
func widestPairOfIndices(nums1 []int, nums2 []int) (ans int) {
127+
d := map[int]int{0: -1}
128+
s := 0
129+
for i := range nums1 {
130+
s += nums1[i] - nums2[i]
131+
if j, ok := d[s]; ok {
132+
ans = max(ans, i-j)
133+
} else {
134+
d[s] = i
135+
}
136+
}
137+
return
138+
}
139+
140+
func max(a, b int) int {
141+
if a > b {
142+
return a
143+
}
144+
return b
145+
}
71146
```
72147

73148
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int widestPairOfIndices(vector<int>& nums1, vector<int>& nums2) {
4+
unordered_map<int, int> d;
5+
d[0] = -1;
6+
int ans = 0, s = 0;
7+
int n = nums1.size();
8+
for (int i = 0; i < n; ++i) {
9+
s += nums1[i] - nums2[i];
10+
if (d.count(s)) {
11+
ans = max(ans, i - d[s]);
12+
} else {
13+
d[s] = i;
14+
}
15+
}
16+
return ans;
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func widestPairOfIndices(nums1 []int, nums2 []int) (ans int) {
2+
d := map[int]int{0: -1}
3+
s := 0
4+
for i := range nums1 {
5+
s += nums1[i] - nums2[i]
6+
if j, ok := d[s]; ok {
7+
ans = max(ans, i-j)
8+
} else {
9+
d[s] = i
10+
}
11+
}
12+
return
13+
}
14+
15+
func max(a, b int) int {
16+
if a > b {
17+
return a
18+
}
19+
return b
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int widestPairOfIndices(int[] nums1, int[] nums2) {
3+
Map<Integer, Integer> d = new HashMap<>();
4+
d.put(0, -1);
5+
int n = nums1.length;
6+
int s = 0;
7+
int ans = 0;
8+
for (int i = 0; i < n; ++i) {
9+
s += nums1[i] - nums2[i];
10+
if (d.containsKey(s)) {
11+
ans = Math.max(ans, i - d.get(s));
12+
} else {
13+
d.put(s, i);
14+
}
15+
}
16+
return ans;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def widestPairOfIndices(self, nums1: List[int], nums2: List[int]) -> int:
3+
d = {0: -1}
4+
ans = s = 0
5+
for i, (a, b) in enumerate(zip(nums1, nums2)):
6+
s += a - b
7+
if s in d:
8+
ans = max(ans, i - d[s])
9+
else:
10+
d[s] = i
11+
return ans

0 commit comments

Comments
 (0)