Skip to content

Commit 89b242b

Browse files
committed
feat: add solutions to lc problem: No.1865. Finding Pairs With a Certain Sum
1 parent e21b17f commit 89b242b

File tree

4 files changed

+170
-6
lines changed

4 files changed

+170
-6
lines changed

solution/1800-1899/1865.Finding Pairs With a Certain Sum/README.md

+58-3
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,82 @@ findSumPairs.count(7); // 返回 11 ;下标对 (2,1), (2,2), (2,4), (3,1), (3
5858
<li>最多调用 <code>add</code> 和 <code>count</code> 函数各 <code>1000</code> 次</li>
5959
</ul>
6060

61-
6261
## 解法
6362

6463
<!-- 这里可写通用的实现逻辑 -->
6564

65+
“哈希表”实现。
66+
6667
<!-- tabs:start -->
6768

6869
### **Python3**
6970

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

7273
```python
73-
74+
class FindSumPairs:
75+
76+
def __init__(self, nums1: List[int], nums2: List[int]):
77+
self.nums1 = nums1
78+
self.nums2 = nums2
79+
self.counter = collections.Counter(nums2)
80+
81+
def add(self, index: int, val: int) -> None:
82+
old_val = self.nums2[index]
83+
self.counter[old_val] -= 1
84+
self.nums2[index] += val
85+
self.counter[old_val + val] += 1
86+
87+
def count(self, tot: int) -> int:
88+
return sum([self.counter[tot - num] for num in self.nums1])
89+
90+
# Your FindSumPairs object will be instantiated and called as such:
91+
# obj = FindSumPairs(nums1, nums2)
92+
# obj.add(index,val)
93+
# param_2 = obj.count(tot)
7494
```
7595

7696
### **Java**
7797

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

80100
```java
81-
101+
class FindSumPairs {
102+
private int[] nums1;
103+
private int[] nums2;
104+
private Map<Integer, Integer> counter;
105+
106+
public FindSumPairs(int[] nums1, int[] nums2) {
107+
this.nums1 = nums1;
108+
this.nums2 = nums2;
109+
counter = new HashMap<>();
110+
for (int num : nums2) {
111+
counter.put(num, counter.getOrDefault(num, 0) + 1);
112+
}
113+
}
114+
115+
public void add(int index, int val) {
116+
int oldVal = nums2[index];
117+
counter.put(oldVal, counter.get(oldVal) - 1);
118+
nums2[index] += val;
119+
counter.put(oldVal + val, counter.getOrDefault(oldVal + val, 0) + 1);
120+
}
121+
122+
public int count(int tot) {
123+
int res = 0;
124+
for (int num : nums1) {
125+
res += counter.getOrDefault(tot - num, 0);
126+
}
127+
return res;
128+
}
129+
}
130+
131+
/**
132+
* Your FindSumPairs object will be instantiated and called as such:
133+
* FindSumPairs obj = new FindSumPairs(nums1, nums2);
134+
* obj.add(index,val);
135+
* int param_2 = obj.count(tot);
136+
*/
82137
```
83138

84139
### **...**

solution/1800-1899/1865.Finding Pairs With a Certain Sum/README_EN.md

+56-3
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,74 @@ findSumPairs.count(7); // return 11; pairs (2,1), (2,2), (2,4), (3,1), (3,2), (
5454
<li>At most <code>1000</code> calls are made to <code>add</code> and <code>count</code> <strong>each</strong>.</li>
5555
</ul>
5656

57-
5857
## Solutions
5958

6059
<!-- tabs:start -->
6160

6261
### **Python3**
6362

6463
```python
65-
64+
class FindSumPairs:
65+
66+
def __init__(self, nums1: List[int], nums2: List[int]):
67+
self.nums1 = nums1
68+
self.nums2 = nums2
69+
self.counter = collections.Counter(nums2)
70+
71+
def add(self, index: int, val: int) -> None:
72+
old_val = self.nums2[index]
73+
self.counter[old_val] -= 1
74+
self.nums2[index] += val
75+
self.counter[old_val + val] += 1
76+
77+
def count(self, tot: int) -> int:
78+
return sum([self.counter[tot - num] for num in self.nums1])
79+
80+
# Your FindSumPairs object will be instantiated and called as such:
81+
# obj = FindSumPairs(nums1, nums2)
82+
# obj.add(index,val)
83+
# param_2 = obj.count(tot)
6684
```
6785

6886
### **Java**
6987

7088
```java
71-
89+
class FindSumPairs {
90+
private int[] nums1;
91+
private int[] nums2;
92+
private Map<Integer, Integer> counter;
93+
94+
public FindSumPairs(int[] nums1, int[] nums2) {
95+
this.nums1 = nums1;
96+
this.nums2 = nums2;
97+
counter = new HashMap<>();
98+
for (int num : nums2) {
99+
counter.put(num, counter.getOrDefault(num, 0) + 1);
100+
}
101+
}
102+
103+
public void add(int index, int val) {
104+
int oldVal = nums2[index];
105+
counter.put(oldVal, counter.get(oldVal) - 1);
106+
nums2[index] += val;
107+
counter.put(oldVal + val, counter.getOrDefault(oldVal + val, 0) + 1);
108+
}
109+
110+
public int count(int tot) {
111+
int res = 0;
112+
for (int num : nums1) {
113+
res += counter.getOrDefault(tot - num, 0);
114+
}
115+
return res;
116+
}
117+
}
118+
119+
/**
120+
* Your FindSumPairs object will be instantiated and called as such:
121+
* FindSumPairs obj = new FindSumPairs(nums1, nums2);
122+
* obj.add(index,val);
123+
* int param_2 = obj.count(tot);
124+
*/
72125
```
73126

74127
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class FindSumPairs {
2+
private int[] nums1;
3+
private int[] nums2;
4+
private Map<Integer, Integer> counter;
5+
6+
public FindSumPairs(int[] nums1, int[] nums2) {
7+
this.nums1 = nums1;
8+
this.nums2 = nums2;
9+
counter = new HashMap<>();
10+
for (int num : nums2) {
11+
counter.put(num, counter.getOrDefault(num, 0) + 1);
12+
}
13+
}
14+
15+
public void add(int index, int val) {
16+
int oldVal = nums2[index];
17+
counter.put(oldVal, counter.get(oldVal) - 1);
18+
nums2[index] += val;
19+
counter.put(oldVal + val, counter.getOrDefault(oldVal + val, 0) + 1);
20+
}
21+
22+
public int count(int tot) {
23+
int res = 0;
24+
for (int num : nums1) {
25+
res += counter.getOrDefault(tot - num, 0);
26+
}
27+
return res;
28+
}
29+
}
30+
31+
/**
32+
* Your FindSumPairs object will be instantiated and called as such:
33+
* FindSumPairs obj = new FindSumPairs(nums1, nums2);
34+
* obj.add(index,val);
35+
* int param_2 = obj.count(tot);
36+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class FindSumPairs:
2+
3+
def __init__(self, nums1: List[int], nums2: List[int]):
4+
self.nums1 = nums1
5+
self.nums2 = nums2
6+
self.counter = collections.Counter(nums2)
7+
8+
def add(self, index: int, val: int) -> None:
9+
old_val = self.nums2[index]
10+
self.counter[old_val] -= 1
11+
self.nums2[index] += val
12+
self.counter[old_val + val] += 1
13+
14+
def count(self, tot: int) -> int:
15+
return sum([self.counter[tot - num] for num in self.nums1])
16+
17+
# Your FindSumPairs object will be instantiated and called as such:
18+
# obj = FindSumPairs(nums1, nums2)
19+
# obj.add(index,val)
20+
# param_2 = obj.count(tot)

0 commit comments

Comments
 (0)