From 670b3960e085349de54fe7957260fa3885a69008 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 7 Feb 2025 14:38:52 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1865 No.1865.Finding Pairs With a Certain Sum --- .../README.md | 200 ++++++++++++++--- .../README_EN.md | 202 +++++++++++++++--- .../Solution.cpp | 15 +- .../Solution.cs | 53 +++++ .../Solution.go | 15 +- .../Solution.java | 15 +- .../Solution.js | 40 ++++ .../Solution.py | 9 +- .../Solution.ts | 33 +++ 9 files changed, 496 insertions(+), 86 deletions(-) create mode 100644 solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.cs create mode 100644 solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.js create mode 100644 solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.ts diff --git a/solution/1800-1899/1865.Finding Pairs With a Certain Sum/README.md b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/README.md index 86e8c527a7aae..80dc383ff6383 100644 --- a/solution/1800-1899/1865.Finding Pairs With a Certain Sum/README.md +++ b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/README.md @@ -80,13 +80,15 @@ findSumPairs.count(7); // 返回 11 ;下标对 (2,1), (2,2), (2,4), (3,1), (3 ### 方法一:哈希表 -我们可以用哈希表 `cnt` 统计数组 `nums2` 中每个数字出现的次数。 +我们注意到,数组 $\textit{nums1}$ 的长度不超过 ${10}^3$,数组 $\textit{nums2}$ 的长度达到 ${10}^5$,因此,如果直接暴力枚举所有下标对 $(i, j)$,计算 $\textit{nums1}[i] + \textit{nums2}[j]$ 是否等于指定值 $\textit{tot}$,那么会超出时间限制。 -对于 `add` 操作,我们需要更新哈希表中 `nums2[index]` 的值,即 `cnt[nums2[index]] -= 1`,然后更新 `nums2[index] += val`,最后更新哈希表中 `nums2[index]` 的值,即 `cnt[nums2[index]] += 1`。 +能否只枚举长度较短的数组 $\textit{nums1}$ 呢?答案是可以的。我们用一个哈希表 $\textit{cnt}$ 统计数组 $\textit{nums2}$ 中每个元素出现的次数,然后枚举数组 $\textit{nums1}$ 中的每个元素 $x$,计算 $\textit{cnt}[\textit{tot} - x]$ 的值之和即可。 -对于 `count` 操作,我们遍历数组 `nums1`,对于每个数字 `v`,我们需要统计满足 `tot - v` 的数字出现的次数,即 `cnt[tot - v]`,然后将其累加到答案中。 +在调用 $\text{add}$ 方法时,我们需要先将 $\textit{nums2}[index]$ 对应的值从 $\textit{cnt}$ 中减去 $1$,然后将 $\textit{nums2}[index]$ 的值加上 $\textit{val}$,最后将 $\textit{nums2}[index]$ 对应的值加上 $1$。 -时间复杂度:对于 `add` 操作,时间复杂度为 $O(1)$,对于 `count` 操作,时间复杂度为 $O(n)$,其中 $n$ 为数组 `nums1` 的长度。空间复杂度 $O(m)$,其中 $m$ 为数组 `nums2` 的长度。 +在调用 $\text{count}$ 方法时,我们只需要遍历数组 $\textit{nums1}$,对于每个元素 $x$,计算 $\textit{cnt}[\textit{tot} - x]$ 的值之和即可。 + +时间复杂度 $O(n \times q)$,空间复杂度 $O(m)$。其中 $n$ 和 $m$ 分别是数组 $\textit{nums1}$ 和 $\textit{nums2}$ 的长度,而 $q$ 是调用 $\text{count}$ 方法的次数。 @@ -94,19 +96,19 @@ findSumPairs.count(7); // 返回 11 ;下标对 (2,1), (2,2), (2,4), (3,1), (3 ```python class FindSumPairs: + def __init__(self, nums1: List[int], nums2: List[int]): + self.cnt = Counter(nums2) self.nums1 = nums1 self.nums2 = nums2 - self.cnt = Counter(nums2) def add(self, index: int, val: int) -> None: - old = self.nums2[index] - self.cnt[old] -= 1 - self.cnt[old + val] += 1 + self.cnt[self.nums2[index]] -= 1 self.nums2[index] += val + self.cnt[self.nums2[index]] += 1 def count(self, tot: int) -> int: - return sum(self.cnt[tot - v] for v in self.nums1) + return sum(self.cnt[tot - x] for x in self.nums1) # Your FindSumPairs object will be instantiated and called as such: @@ -126,22 +128,21 @@ class FindSumPairs { public FindSumPairs(int[] nums1, int[] nums2) { this.nums1 = nums1; this.nums2 = nums2; - for (int v : nums2) { - cnt.put(v, cnt.getOrDefault(v, 0) + 1); + for (int x : nums2) { + cnt.merge(x, 1, Integer::sum); } } public void add(int index, int val) { - int old = nums2[index]; - cnt.put(old, cnt.get(old) - 1); - cnt.put(old + val, cnt.getOrDefault(old + val, 0) + 1); + cnt.merge(nums2[index], -1, Integer::sum); nums2[index] += val; + cnt.merge(nums2[index], 1, Integer::sum); } public int count(int tot) { int ans = 0; - for (int v : nums1) { - ans += cnt.getOrDefault(tot - v, 0); + for (int x : nums1) { + ans += cnt.getOrDefault(tot - x, 0); } return ans; } @@ -163,22 +164,21 @@ public: FindSumPairs(vector& nums1, vector& nums2) { this->nums1 = nums1; this->nums2 = nums2; - for (int& v : nums2) { - ++cnt[v]; + for (int x : nums2) { + ++cnt[x]; } } void add(int index, int val) { - int old = nums2[index]; - --cnt[old]; - ++cnt[old + val]; + --cnt[nums2[index]]; nums2[index] += val; + ++cnt[nums2[index]]; } int count(int tot) { int ans = 0; - for (int& v : nums1) { - ans += cnt[tot - v]; + for (int x : nums1) { + ans += cnt[tot - x]; } return ans; } @@ -208,22 +208,21 @@ type FindSumPairs struct { func Constructor(nums1 []int, nums2 []int) FindSumPairs { cnt := map[int]int{} - for _, v := range nums2 { - cnt[v]++ + for _, x := range nums2 { + cnt[x]++ } return FindSumPairs{nums1, nums2, cnt} } func (this *FindSumPairs) Add(index int, val int) { - old := this.nums2[index] - this.cnt[old]-- - this.cnt[old+val]++ + this.cnt[this.nums2[index]]-- this.nums2[index] += val + this.cnt[this.nums2[index]]++ } func (this *FindSumPairs) Count(tot int) (ans int) { - for _, v := range this.nums1 { - ans += this.cnt[tot-v] + for _, x := range this.nums1 { + ans += this.cnt[tot-x] } return } @@ -236,6 +235,147 @@ func (this *FindSumPairs) Count(tot int) (ans int) { */ ``` +#### TypeScript + +```ts +class FindSumPairs { + private nums1: number[]; + private nums2: number[]; + private cnt: Map; + + constructor(nums1: number[], nums2: number[]) { + this.nums1 = nums1; + this.nums2 = nums2; + this.cnt = new Map(); + for (const x of nums2) { + this.cnt.set(x, (this.cnt.get(x) || 0) + 1); + } + } + + add(index: number, val: number): void { + const old = this.nums2[index]; + this.cnt.set(old, this.cnt.get(old)! - 1); + this.nums2[index] += val; + const now = this.nums2[index]; + this.cnt.set(now, (this.cnt.get(now) || 0) + 1); + } + + count(tot: number): number { + return this.nums1.reduce((acc, x) => acc + (this.cnt.get(tot - x) || 0), 0); + } +} + +/** + * Your FindSumPairs object will be instantiated and called as such: + * var obj = new FindSumPairs(nums1, nums2) + * obj.add(index,val) + * var param_2 = obj.count(tot) + */ +``` + +#### JavaScript + +```js +/** + * @param {number[]} nums1 + * @param {number[]} nums2 + */ +var FindSumPairs = function (nums1, nums2) { + this.nums1 = nums1; + this.nums2 = nums2; + this.cnt = new Map(); + for (const x of nums2) { + this.cnt.set(x, (this.cnt.get(x) || 0) + 1); + } +}; + +/** + * @param {number} index + * @param {number} val + * @return {void} + */ +FindSumPairs.prototype.add = function (index, val) { + const old = this.nums2[index]; + this.cnt.set(old, this.cnt.get(old) - 1); + this.nums2[index] += val; + const now = this.nums2[index]; + this.cnt.set(now, (this.cnt.get(now) || 0) + 1); +}; + +/** + * @param {number} tot + * @return {number} + */ +FindSumPairs.prototype.count = function (tot) { + return this.nums1.reduce((acc, x) => acc + (this.cnt.get(tot - x) || 0), 0); +}; + +/** + * Your FindSumPairs object will be instantiated and called as such: + * var obj = new FindSumPairs(nums1, nums2) + * obj.add(index,val) + * var param_2 = obj.count(tot) + */ +``` + +#### C# + +```cs +public class FindSumPairs { + private int[] nums1; + private int[] nums2; + private Dictionary cnt = new Dictionary(); + + public FindSumPairs(int[] nums1, int[] nums2) { + this.nums1 = nums1; + this.nums2 = nums2; + foreach (int x in nums2) { + if (cnt.ContainsKey(x)) { + cnt[x]++; + } else { + cnt[x] = 1; + } + } + } + + public void Add(int index, int val) { + int oldVal = nums2[index]; + if (cnt.TryGetValue(oldVal, out int oldCount)) { + if (oldCount == 1) { + cnt.Remove(oldVal); + } else { + cnt[oldVal] = oldCount - 1; + } + } + nums2[index] += val; + int newVal = nums2[index]; + if (cnt.TryGetValue(newVal, out int newCount)) { + cnt[newVal] = newCount + 1; + } else { + cnt[newVal] = 1; + } + } + + public int Count(int tot) { + int ans = 0; + foreach (int x in nums1) { + int target = tot - x; + if (cnt.TryGetValue(target, out int count)) { + ans += count; + } + } + return ans; + } +} + +/** + * Your FindSumPairs object will be instantiated and called as such: + * FindSumPairs obj = new FindSumPairs(nums1, nums2); + * obj.Add(index,val); + * int param_2 = obj.Count(tot); + */ +``` + diff --git a/solution/1800-1899/1865.Finding Pairs With a Certain Sum/README_EN.md b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/README_EN.md index 036e96217617f..37f4847d8a54f 100644 --- a/solution/1800-1899/1865.Finding Pairs With a Certain Sum/README_EN.md +++ b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/README_EN.md @@ -76,7 +76,17 @@ findSumPairs.count(7); // return 11; pairs (2,1), (2,2), (2,4), (3,1), (3,2), ( -### Solution 1 +### Solution 1: Hash Table + +We note that the length of the array $\textit{nums1}$ does not exceed ${10}^3$, while the length of the array $\textit{nums2}$ reaches ${10}^5$. Therefore, if we directly enumerate all index pairs $(i, j)$ and check whether $\textit{nums1}[i] + \textit{nums2}[j]$ equals the specified value $\textit{tot}$, it will exceed the time limit. + +Can we only enumerate the shorter array $\textit{nums1}$? The answer is yes. We use a hash table $\textit{cnt}$ to count the occurrences of each element in the array $\textit{nums2}$, then enumerate each element $x$ in the array $\textit{nums1}$ and calculate the sum of $\textit{cnt}[\textit{tot} - x]$. + +When calling the $\text{add}$ method, we need to first decrement the value corresponding to $\textit{nums2}[index]$ in $\textit{cnt}$ by $1$, then add $\textit{val}$ to the value of $\textit{nums2}[index]$, and finally increment the value corresponding to $\textit{nums2}[index]$ in $\textit{cnt}$ by $1$. + +When calling the $\text{count}$ method, we only need to traverse the array $\textit{nums1}$ and calculate the sum of $\textit{cnt}[\textit{tot} - x]$ for each element $x$. + +The time complexity is $O(n \times q)$, and the space complexity is $O(m)$. Here, $n$ and $m$ are the lengths of the arrays $\textit{nums1}$ and $\textit{nums2}$, respectively, and $q$ is the number of times the $\text{count}$ method is called. @@ -84,19 +94,19 @@ findSumPairs.count(7); // return 11; pairs (2,1), (2,2), (2,4), (3,1), (3,2), ( ```python class FindSumPairs: + def __init__(self, nums1: List[int], nums2: List[int]): + self.cnt = Counter(nums2) self.nums1 = nums1 self.nums2 = nums2 - self.cnt = Counter(nums2) def add(self, index: int, val: int) -> None: - old = self.nums2[index] - self.cnt[old] -= 1 - self.cnt[old + val] += 1 + self.cnt[self.nums2[index]] -= 1 self.nums2[index] += val + self.cnt[self.nums2[index]] += 1 def count(self, tot: int) -> int: - return sum(self.cnt[tot - v] for v in self.nums1) + return sum(self.cnt[tot - x] for x in self.nums1) # Your FindSumPairs object will be instantiated and called as such: @@ -116,22 +126,21 @@ class FindSumPairs { public FindSumPairs(int[] nums1, int[] nums2) { this.nums1 = nums1; this.nums2 = nums2; - for (int v : nums2) { - cnt.put(v, cnt.getOrDefault(v, 0) + 1); + for (int x : nums2) { + cnt.merge(x, 1, Integer::sum); } } public void add(int index, int val) { - int old = nums2[index]; - cnt.put(old, cnt.get(old) - 1); - cnt.put(old + val, cnt.getOrDefault(old + val, 0) + 1); + cnt.merge(nums2[index], -1, Integer::sum); nums2[index] += val; + cnt.merge(nums2[index], 1, Integer::sum); } public int count(int tot) { int ans = 0; - for (int v : nums1) { - ans += cnt.getOrDefault(tot - v, 0); + for (int x : nums1) { + ans += cnt.getOrDefault(tot - x, 0); } return ans; } @@ -153,22 +162,21 @@ public: FindSumPairs(vector& nums1, vector& nums2) { this->nums1 = nums1; this->nums2 = nums2; - for (int& v : nums2) { - ++cnt[v]; + for (int x : nums2) { + ++cnt[x]; } } void add(int index, int val) { - int old = nums2[index]; - --cnt[old]; - ++cnt[old + val]; + --cnt[nums2[index]]; nums2[index] += val; + ++cnt[nums2[index]]; } int count(int tot) { int ans = 0; - for (int& v : nums1) { - ans += cnt[tot - v]; + for (int x : nums1) { + ans += cnt[tot - x]; } return ans; } @@ -198,22 +206,21 @@ type FindSumPairs struct { func Constructor(nums1 []int, nums2 []int) FindSumPairs { cnt := map[int]int{} - for _, v := range nums2 { - cnt[v]++ + for _, x := range nums2 { + cnt[x]++ } return FindSumPairs{nums1, nums2, cnt} } func (this *FindSumPairs) Add(index int, val int) { - old := this.nums2[index] - this.cnt[old]-- - this.cnt[old+val]++ + this.cnt[this.nums2[index]]-- this.nums2[index] += val + this.cnt[this.nums2[index]]++ } func (this *FindSumPairs) Count(tot int) (ans int) { - for _, v := range this.nums1 { - ans += this.cnt[tot-v] + for _, x := range this.nums1 { + ans += this.cnt[tot-x] } return } @@ -226,6 +233,147 @@ func (this *FindSumPairs) Count(tot int) (ans int) { */ ``` +#### TypeScript + +```ts +class FindSumPairs { + private nums1: number[]; + private nums2: number[]; + private cnt: Map; + + constructor(nums1: number[], nums2: number[]) { + this.nums1 = nums1; + this.nums2 = nums2; + this.cnt = new Map(); + for (const x of nums2) { + this.cnt.set(x, (this.cnt.get(x) || 0) + 1); + } + } + + add(index: number, val: number): void { + const old = this.nums2[index]; + this.cnt.set(old, this.cnt.get(old)! - 1); + this.nums2[index] += val; + const now = this.nums2[index]; + this.cnt.set(now, (this.cnt.get(now) || 0) + 1); + } + + count(tot: number): number { + return this.nums1.reduce((acc, x) => acc + (this.cnt.get(tot - x) || 0), 0); + } +} + +/** + * Your FindSumPairs object will be instantiated and called as such: + * var obj = new FindSumPairs(nums1, nums2) + * obj.add(index,val) + * var param_2 = obj.count(tot) + */ +``` + +#### JavaScript + +```js +/** + * @param {number[]} nums1 + * @param {number[]} nums2 + */ +var FindSumPairs = function (nums1, nums2) { + this.nums1 = nums1; + this.nums2 = nums2; + this.cnt = new Map(); + for (const x of nums2) { + this.cnt.set(x, (this.cnt.get(x) || 0) + 1); + } +}; + +/** + * @param {number} index + * @param {number} val + * @return {void} + */ +FindSumPairs.prototype.add = function (index, val) { + const old = this.nums2[index]; + this.cnt.set(old, this.cnt.get(old) - 1); + this.nums2[index] += val; + const now = this.nums2[index]; + this.cnt.set(now, (this.cnt.get(now) || 0) + 1); +}; + +/** + * @param {number} tot + * @return {number} + */ +FindSumPairs.prototype.count = function (tot) { + return this.nums1.reduce((acc, x) => acc + (this.cnt.get(tot - x) || 0), 0); +}; + +/** + * Your FindSumPairs object will be instantiated and called as such: + * var obj = new FindSumPairs(nums1, nums2) + * obj.add(index,val) + * var param_2 = obj.count(tot) + */ +``` + +#### C# + +```cs +public class FindSumPairs { + private int[] nums1; + private int[] nums2; + private Dictionary cnt = new Dictionary(); + + public FindSumPairs(int[] nums1, int[] nums2) { + this.nums1 = nums1; + this.nums2 = nums2; + foreach (int x in nums2) { + if (cnt.ContainsKey(x)) { + cnt[x]++; + } else { + cnt[x] = 1; + } + } + } + + public void Add(int index, int val) { + int oldVal = nums2[index]; + if (cnt.TryGetValue(oldVal, out int oldCount)) { + if (oldCount == 1) { + cnt.Remove(oldVal); + } else { + cnt[oldVal] = oldCount - 1; + } + } + nums2[index] += val; + int newVal = nums2[index]; + if (cnt.TryGetValue(newVal, out int newCount)) { + cnt[newVal] = newCount + 1; + } else { + cnt[newVal] = 1; + } + } + + public int Count(int tot) { + int ans = 0; + foreach (int x in nums1) { + int target = tot - x; + if (cnt.TryGetValue(target, out int count)) { + ans += count; + } + } + return ans; + } +} + +/** + * Your FindSumPairs object will be instantiated and called as such: + * FindSumPairs obj = new FindSumPairs(nums1, nums2); + * obj.Add(index,val); + * int param_2 = obj.Count(tot); + */ +``` + diff --git a/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.cpp b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.cpp index a474eb82eea34..142812fa7ded0 100644 --- a/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.cpp +++ b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.cpp @@ -3,22 +3,21 @@ class FindSumPairs { FindSumPairs(vector& nums1, vector& nums2) { this->nums1 = nums1; this->nums2 = nums2; - for (int& v : nums2) { - ++cnt[v]; + for (int x : nums2) { + ++cnt[x]; } } void add(int index, int val) { - int old = nums2[index]; - --cnt[old]; - ++cnt[old + val]; + --cnt[nums2[index]]; nums2[index] += val; + ++cnt[nums2[index]]; } int count(int tot) { int ans = 0; - for (int& v : nums1) { - ans += cnt[tot - v]; + for (int x : nums1) { + ans += cnt[tot - x]; } return ans; } @@ -34,4 +33,4 @@ class FindSumPairs { * FindSumPairs* obj = new FindSumPairs(nums1, nums2); * obj->add(index,val); * int param_2 = obj->count(tot); - */ \ No newline at end of file + */ diff --git a/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.cs b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.cs new file mode 100644 index 0000000000000..78e9bb108c1c0 --- /dev/null +++ b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.cs @@ -0,0 +1,53 @@ +public class FindSumPairs { + private int[] nums1; + private int[] nums2; + private Dictionary cnt = new Dictionary(); + + public FindSumPairs(int[] nums1, int[] nums2) { + this.nums1 = nums1; + this.nums2 = nums2; + foreach (int x in nums2) { + if (cnt.ContainsKey(x)) { + cnt[x]++; + } else { + cnt[x] = 1; + } + } + } + + public void Add(int index, int val) { + int oldVal = nums2[index]; + if (cnt.TryGetValue(oldVal, out int oldCount)) { + if (oldCount == 1) { + cnt.Remove(oldVal); + } else { + cnt[oldVal] = oldCount - 1; + } + } + nums2[index] += val; + int newVal = nums2[index]; + if (cnt.TryGetValue(newVal, out int newCount)) { + cnt[newVal] = newCount + 1; + } else { + cnt[newVal] = 1; + } + } + + public int Count(int tot) { + int ans = 0; + foreach (int x in nums1) { + int target = tot - x; + if (cnt.TryGetValue(target, out int count)) { + ans += count; + } + } + return ans; + } +} + +/** + * Your FindSumPairs object will be instantiated and called as such: + * FindSumPairs obj = new FindSumPairs(nums1, nums2); + * obj.Add(index,val); + * int param_2 = obj.Count(tot); + */ diff --git a/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.go b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.go index bb7189881b828..53de11896091d 100644 --- a/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.go +++ b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.go @@ -6,22 +6,21 @@ type FindSumPairs struct { func Constructor(nums1 []int, nums2 []int) FindSumPairs { cnt := map[int]int{} - for _, v := range nums2 { - cnt[v]++ + for _, x := range nums2 { + cnt[x]++ } return FindSumPairs{nums1, nums2, cnt} } func (this *FindSumPairs) Add(index int, val int) { - old := this.nums2[index] - this.cnt[old]-- - this.cnt[old+val]++ + this.cnt[this.nums2[index]]-- this.nums2[index] += val + this.cnt[this.nums2[index]]++ } func (this *FindSumPairs) Count(tot int) (ans int) { - for _, v := range this.nums1 { - ans += this.cnt[tot-v] + for _, x := range this.nums1 { + ans += this.cnt[tot-x] } return } @@ -31,4 +30,4 @@ func (this *FindSumPairs) Count(tot int) (ans int) { * obj := Constructor(nums1, nums2); * obj.Add(index,val); * param_2 := obj.Count(tot); - */ \ No newline at end of file + */ diff --git a/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.java b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.java index 291a784d5a395..c0c6bd0f3db09 100644 --- a/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.java +++ b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.java @@ -6,22 +6,21 @@ class FindSumPairs { public FindSumPairs(int[] nums1, int[] nums2) { this.nums1 = nums1; this.nums2 = nums2; - for (int v : nums2) { - cnt.put(v, cnt.getOrDefault(v, 0) + 1); + for (int x : nums2) { + cnt.merge(x, 1, Integer::sum); } } public void add(int index, int val) { - int old = nums2[index]; - cnt.put(old, cnt.get(old) - 1); - cnt.put(old + val, cnt.getOrDefault(old + val, 0) + 1); + cnt.merge(nums2[index], -1, Integer::sum); nums2[index] += val; + cnt.merge(nums2[index], 1, Integer::sum); } public int count(int tot) { int ans = 0; - for (int v : nums1) { - ans += cnt.getOrDefault(tot - v, 0); + for (int x : nums1) { + ans += cnt.getOrDefault(tot - x, 0); } return ans; } @@ -32,4 +31,4 @@ public int count(int tot) { * FindSumPairs obj = new FindSumPairs(nums1, nums2); * obj.add(index,val); * int param_2 = obj.count(tot); - */ \ No newline at end of file + */ diff --git a/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.js b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.js new file mode 100644 index 0000000000000..225170b2308cc --- /dev/null +++ b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.js @@ -0,0 +1,40 @@ +/** + * @param {number[]} nums1 + * @param {number[]} nums2 + */ +var FindSumPairs = function (nums1, nums2) { + this.nums1 = nums1; + this.nums2 = nums2; + this.cnt = new Map(); + for (const x of nums2) { + this.cnt.set(x, (this.cnt.get(x) || 0) + 1); + } +}; + +/** + * @param {number} index + * @param {number} val + * @return {void} + */ +FindSumPairs.prototype.add = function (index, val) { + const old = this.nums2[index]; + this.cnt.set(old, this.cnt.get(old) - 1); + this.nums2[index] += val; + const now = this.nums2[index]; + this.cnt.set(now, (this.cnt.get(now) || 0) + 1); +}; + +/** + * @param {number} tot + * @return {number} + */ +FindSumPairs.prototype.count = function (tot) { + return this.nums1.reduce((acc, x) => acc + (this.cnt.get(tot - x) || 0), 0); +}; + +/** + * Your FindSumPairs object will be instantiated and called as such: + * var obj = new FindSumPairs(nums1, nums2) + * obj.add(index,val) + * var param_2 = obj.count(tot) + */ diff --git a/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.py b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.py index 15cf8387f8986..1fc268fec35c2 100644 --- a/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.py +++ b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.py @@ -1,17 +1,16 @@ class FindSumPairs: def __init__(self, nums1: List[int], nums2: List[int]): + self.cnt = Counter(nums2) self.nums1 = nums1 self.nums2 = nums2 - self.cnt = Counter(nums2) def add(self, index: int, val: int) -> None: - old = self.nums2[index] - self.cnt[old] -= 1 - self.cnt[old + val] += 1 + self.cnt[self.nums2[index]] -= 1 self.nums2[index] += val + self.cnt[self.nums2[index]] += 1 def count(self, tot: int) -> int: - return sum(self.cnt[tot - v] for v in self.nums1) + return sum(self.cnt[tot - x] for x in self.nums1) # Your FindSumPairs object will be instantiated and called as such: diff --git a/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.ts b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.ts new file mode 100644 index 0000000000000..695630e229daf --- /dev/null +++ b/solution/1800-1899/1865.Finding Pairs With a Certain Sum/Solution.ts @@ -0,0 +1,33 @@ +class FindSumPairs { + private nums1: number[]; + private nums2: number[]; + private cnt: Map; + + constructor(nums1: number[], nums2: number[]) { + this.nums1 = nums1; + this.nums2 = nums2; + this.cnt = new Map(); + for (const x of nums2) { + this.cnt.set(x, (this.cnt.get(x) || 0) + 1); + } + } + + add(index: number, val: number): void { + const old = this.nums2[index]; + this.cnt.set(old, this.cnt.get(old)! - 1); + this.nums2[index] += val; + const now = this.nums2[index]; + this.cnt.set(now, (this.cnt.get(now) || 0) + 1); + } + + count(tot: number): number { + return this.nums1.reduce((acc, x) => acc + (this.cnt.get(tot - x) || 0), 0); + } +} + +/** + * Your FindSumPairs object will be instantiated and called as such: + * var obj = new FindSumPairs(nums1, nums2) + * obj.add(index,val) + * var param_2 = obj.count(tot) + */