Skip to content

Commit cd41148

Browse files
committed
Sync LeetCode submission Runtime - 293 ms (47.37%), Memory - 48.3 MB (82.81%)
1 parent 8d85100 commit cd41148

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code>. You are tasked to implement a data structure that supports queries of two types:</p>
2+
3+
<ol>
4+
<li><strong>Add</strong> a positive integer to an element of a given index in the array <code>nums2</code>.</li>
5+
<li><strong>Count</strong> the number of pairs <code>(i, j)</code> such that <code>nums1[i] + nums2[j]</code> equals a given value (<code>0 &lt;= i &lt; nums1.length</code> and <code>0 &lt;= j &lt; nums2.length</code>).</li>
6+
</ol>
7+
8+
<p>Implement the <code>FindSumPairs</code> class:</p>
9+
10+
<ul>
11+
<li><code>FindSumPairs(int[] nums1, int[] nums2)</code> Initializes the <code>FindSumPairs</code> object with two integer arrays <code>nums1</code> and <code>nums2</code>.</li>
12+
<li><code>void add(int index, int val)</code> Adds <code>val</code> to <code>nums2[index]</code>, i.e., apply <code>nums2[index] += val</code>.</li>
13+
<li><code>int count(int tot)</code> Returns the number of pairs <code>(i, j)</code> such that <code>nums1[i] + nums2[j] == tot</code>.</li>
14+
</ul>
15+
16+
<p>&nbsp;</p>
17+
<p><strong class="example">Example 1:</strong></p>
18+
19+
<pre>
20+
<strong>Input</strong>
21+
[&quot;FindSumPairs&quot;, &quot;count&quot;, &quot;add&quot;, &quot;count&quot;, &quot;count&quot;, &quot;add&quot;, &quot;add&quot;, &quot;count&quot;]
22+
[[[1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]], [7], [3, 2], [8], [4], [0, 1], [1, 1], [7]]
23+
<strong>Output</strong>
24+
[null, 8, null, 2, 1, null, null, 11]
25+
26+
<strong>Explanation</strong>
27+
FindSumPairs findSumPairs = new FindSumPairs([1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]);
28+
findSumPairs.count(7); // return 8; pairs (2,2), (3,2), (4,2), (2,4), (3,4), (4,4) make 2 + 5 and pairs (5,1), (5,5) make 3 + 4
29+
findSumPairs.add(3, 2); // now nums2 = [1,4,5,<strong><u>4</u></strong><code>,5,4</code>]
30+
findSumPairs.count(8); // return 2; pairs (5,2), (5,4) make 3 + 5
31+
findSumPairs.count(4); // return 1; pair (5,0) makes 3 + 1
32+
findSumPairs.add(0, 1); // now nums2 = [<strong><u><code>2</code></u></strong>,4,5,4<code>,5,4</code>]
33+
findSumPairs.add(1, 1); // now nums2 = [<code>2</code>,<strong><u>5</u></strong>,5,4<code>,5,4</code>]
34+
findSumPairs.count(7); // return 11; pairs (2,1), (2,2), (2,4), (3,1), (3,2), (3,4), (4,1), (4,2), (4,4) make 2 + 5 and pairs (5,3), (5,5) make 3 + 4
35+
</pre>
36+
37+
<p>&nbsp;</p>
38+
<p><strong>Constraints:</strong></p>
39+
40+
<ul>
41+
<li><code>1 &lt;= nums1.length &lt;= 1000</code></li>
42+
<li><code>1 &lt;= nums2.length &lt;= 10<sup>5</sup></code></li>
43+
<li><code>1 &lt;= nums1[i] &lt;= 10<sup>9</sup></code></li>
44+
<li><code>1 &lt;= nums2[i] &lt;= 10<sup>5</sup></code></li>
45+
<li><code>0 &lt;= index &lt; nums2.length</code></li>
46+
<li><code>1 &lt;= val &lt;= 10<sup>5</sup></code></li>
47+
<li><code>1 &lt;= tot &lt;= 10<sup>9</sup></code></li>
48+
<li>At most <code>1000</code> calls are made to <code>add</code> and <code>count</code> <strong>each</strong>.</li>
49+
</ul>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Approach: Hash Table
2+
3+
from collections import Counter
4+
5+
class FindSumPairs:
6+
7+
def __init__(self, nums1: List[int], nums2: List[int]):
8+
self.nums1 = nums1
9+
self.nums2 = nums2
10+
self.cnt = Counter(nums2)
11+
12+
13+
def add(self, index: int, val: int) -> None:
14+
_nums2, _cnt = self.nums2, self.cnt
15+
_cnt[_nums2[index]] -= 1
16+
_nums2[index] += val
17+
_cnt[_nums2[index]] += 1
18+
19+
20+
def count(self, tot: int) -> int:
21+
_nums1, _cnt = self.nums1, self.cnt
22+
ans = 0
23+
for num in _nums1:
24+
if (rest := tot - num) in _cnt:
25+
ans += _cnt[rest]
26+
return ans
27+
28+
29+
# Your FindSumPairs object will be instantiated and called as such:
30+
# obj = FindSumPairs(nums1, nums2)
31+
# obj.add(index,val)
32+
# param_2 = obj.count(tot)

0 commit comments

Comments
 (0)