Skip to content

Commit 9cb36a0

Browse files
authored
Create 1865-finding-pairs-with-a-certain-sum.js
1 parent 7489b95 commit 9cb36a0

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
/**
3+
* @param {number[]} nums1
4+
* @param {number[]} nums2
5+
*/
6+
const FindSumPairs = function(nums1, nums2) {
7+
this.nums1 = nums1
8+
this.nums2 = nums2
9+
const m = nums1.length, n = nums2.length
10+
this.mp = {}
11+
for(let x of nums2) {
12+
if(this.mp[x] == null) this.mp[x] = 0
13+
this.mp[x]++
14+
}
15+
};
16+
17+
/**
18+
* @param {number} index
19+
* @param {number} val
20+
* @return {void}
21+
*/
22+
FindSumPairs.prototype.add = function(index, val) {
23+
if(val !== 0) {
24+
if(!(--this.mp[this.nums2[index]])) delete this.mp[this.nums2[index]]
25+
}
26+
this.nums2[index] += val
27+
if(this.mp[this.nums2[index]] == null) this.mp[this.nums2[index]] = 0
28+
if(val !== 0)this.mp[this.nums2[index]]++
29+
};
30+
31+
/**
32+
* @param {number} tot
33+
* @return {number}
34+
*/
35+
FindSumPairs.prototype.count = function(tot) {
36+
let ans = 0;
37+
for (let x of this.nums1) {
38+
let res = tot - x;
39+
if (!this.mp[res]) continue;
40+
ans += this.mp[res];
41+
}
42+
return ans;
43+
};
44+
45+
/**
46+
* Your FindSumPairs object will be instantiated and called as such:
47+
* var obj = new FindSumPairs(nums1, nums2)
48+
* obj.add(index,val)
49+
* var param_2 = obj.count(tot)
50+
*/

0 commit comments

Comments
 (0)