|
1 | 1 | # 1865. Finding Pairs With a Certain Sum [Rating: 1680.82]
|
2 | 2 |
|
3 |
| -You are given two integer arrays `nums1` and `nums2`. You are tasked to implement a data structure that supports queries of two types: |
| 3 | +<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> |
4 | 4 |
|
5 |
| -1. **Add** a positive integer to an element of a given index in the array `nums2`. |
6 |
| -2. **Count** the number of pairs `(i, j)` such that `nums1[i] + nums2[j]`equals a given value (`0 <= i < nums1.length` and `0 <= j < nums2.length`). |
| 5 | +<ol> |
| 6 | + <li><strong>Add</strong> a positive integer to an element of a given index in the array <code>nums2</code>.</li> |
| 7 | + <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 <= i < nums1.length</code> and <code>0 <= j < nums2.length</code>).</li> |
| 8 | +</ol> |
7 | 9 |
|
8 |
| -Implement the `FindSumPairs` class: |
| 10 | +<p>Implement the <code>FindSumPairs</code> class:</p> |
9 | 11 |
|
10 |
| -- `FindSumPairs(int[] nums1, int[] nums2)` Initializes the `FindSumPairs` object with two integer arrays `nums1` and `nums2`. |
11 |
| -- `void add(int index, int val)` Adds `val` to `nums2[index]`, i.e., apply `nums2[index] += val`. |
12 |
| -- `int count(int tot)` Returns the number of pairs `(i, j)` such that `nums1[i] + nums2[j] == tot`. |
| 12 | +<ul> |
| 13 | + <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> |
| 14 | + <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> |
| 15 | + <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> |
| 16 | +</ul> |
13 | 17 |
|
14 |
| - |
| 18 | +<p> </p> |
| 19 | +<p><strong class="example">Example 1:</strong></p> |
15 | 20 |
|
16 |
| -**Example 1:** |
17 |
| - |
18 |
| -``` |
19 |
| -Input |
20 |
| -["FindSumPairs", "count", "add", "count", "count", "add", "add", "count"] |
| 21 | +<pre> |
| 22 | +<strong>Input</strong> |
| 23 | +["FindSumPairs", "count", "add", "count", "count", "add", "add", "count"] |
21 | 24 | [[[1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]], [7], [3, 2], [8], [4], [0, 1], [1, 1], [7]]
|
22 |
| -Output |
| 25 | +<strong>Output</strong> |
23 | 26 | [null, 8, null, 2, 1, null, null, 11]
|
24 | 27 |
|
25 |
| -Explanation |
| 28 | +<strong>Explanation</strong> |
26 | 29 | FindSumPairs findSumPairs = new FindSumPairs([1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]);
|
27 | 30 | 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
|
28 |
| -findSumPairs.add(3, 2); // now nums2 = [1,4,5,4,5,4] |
| 31 | +findSumPairs.add(3, 2); // now nums2 = [1,4,5,<strong><u>4</u></strong><code>,5,4</code>] |
29 | 32 | findSumPairs.count(8); // return 2; pairs (5,2), (5,4) make 3 + 5
|
30 | 33 | findSumPairs.count(4); // return 1; pair (5,0) makes 3 + 1
|
31 |
| -findSumPairs.add(0, 1); // now nums2 = [2,4,5,4,5,4] |
32 |
| -findSumPairs.add(1, 1); // now nums2 = [2,5,5,4,5,4] |
| 34 | +findSumPairs.add(0, 1); // now nums2 = [<strong><u><code>2</code></u></strong>,4,5,4<code>,5,4</code>] |
| 35 | +findSumPairs.add(1, 1); // now nums2 = [<code>2</code>,<strong><u>5</u></strong>,5,4<code>,5,4</code>] |
33 | 36 | 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
|
34 |
| -``` |
35 |
| - |
36 |
| - |
37 |
| - |
38 |
| -**Constraints:** |
39 |
| - |
40 |
| -- `1 <= nums1.length <= 1000` |
41 |
| -- 1 <= nums2.length <= 10<sup>5</sup> |
42 |
| -- 1 <= nums1[i] <= 10<sup>9</sup> |
43 |
| -- 1 <= nums2[i] <= 10<sup>5</sup> |
44 |
| -- `0 <= index < nums2.length` |
45 |
| -- 1 <= val <= 10<sup>5</sup> |
46 |
| -- 1 <= tot <= 10<sup>9</sup> |
47 |
| -- At most `1000` calls are made to `add` and `count` **each**. |
| 37 | +</pre> |
| 38 | + |
| 39 | +<p> </p> |
| 40 | +<p><strong>Constraints:</strong></p> |
| 41 | + |
| 42 | +<ul> |
| 43 | + <li><code>1 <= nums1.length <= 1000</code></li> |
| 44 | + <li><code>1 <= nums2.length <= 10<sup>5</sup></code></li> |
| 45 | + <li><code>1 <= nums1[i] <= 10<sup>9</sup></code></li> |
| 46 | + <li><code>1 <= nums2[i] <= 10<sup>5</sup></code></li> |
| 47 | + <li><code>0 <= index < nums2.length</code></li> |
| 48 | + <li><code>1 <= val <= 10<sup>5</sup></code></li> |
| 49 | + <li><code>1 <= tot <= 10<sup>9</sup></code></li> |
| 50 | + <li>At most <code>1000</code> calls are made to <code>add</code> and <code>count</code> <strong>each</strong>.</li> |
| 51 | +</ul> |
0 commit comments