Skip to content

Commit 4476ea9

Browse files
committed
test: [20250706] Add (1865)
1 parent 99913cc commit 4476ea9

File tree

13 files changed

+421
-37
lines changed

13 files changed

+421
-37
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ members = [
247247
"problems/problems_3304",
248248
"problems/problems_3307",
249249
"problems/problems_1394",
250+
"problems/problems_1865",
250251
]
251252

252253
[package]
@@ -516,3 +517,4 @@ solution_779 = { path = "problems/problems_779", features = ["solution_779"] }
516517
solution_3304 = { path = "problems/problems_3304", features = ["solution_3304"] }
517518
solution_3307 = { path = "problems/problems_3307", features = ["solution_3307"] }
518519
solution_1394 = { path = "problems/problems_1394", features = ["solution_1394"] }
520+
solution_1865 = { path = "problems/problems_1865", features = ["solution_1865"] }

daily-problems.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"daily": "1784",
2+
"daily": "1865",
33
"plans": ["3597", "problems", "3598", "problems", "3599", "problems", "3600", "problems"]
44
}

golang/solution_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package golang
22

33
import (
4-
problem "leetCode/problems/problems_1784"
4+
problem "leetCode/problems/problems_1865"
55
"testing"
66
)
77

88
func TestSolution(t *testing.T) {
9-
TestEach(t, "1784", "problems", problem.Solve)
9+
TestEach(t, "1865", "problems", problem.Solve)
1010
}

problems/problems_1865/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "solution_1865"
3+
version = "0.1.0"
4+
edition = "2021"
5+
rust-version = "1.79.0"
6+
authors = ["benhao"]
7+
description = "LeetCode Solution 1865 in Rust"
8+
readme = "../../README.md"
9+
10+
[features]
11+
solution_1865 = []
12+
13+
[dependencies]
14+
serde_json = "1.0"
15+
rand = "0.8.4"
16+
regex = "1.10.5"
17+
library = { path = "../../rust/library", features = ["model"] }
18+
19+
[lib]
20+
name = "solution_1865"
21+
path = "solution.rs"

problems/problems_1865/Solution.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//go:build ignore
2+
#include "cpp/common/Solution.h"
3+
4+
5+
using namespace std;
6+
using json = nlohmann::json;
7+
8+
class FindSumPairs {
9+
public:
10+
FindSumPairs(vector<int>& nums1, vector<int>& nums2) {
11+
12+
}
13+
14+
void add(int index, int val) {
15+
16+
}
17+
18+
int count(int tot) {
19+
20+
}
21+
};
22+
23+
/**
24+
* Your FindSumPairs object will be instantiated and called as such:
25+
* FindSumPairs* obj = new FindSumPairs(nums1, nums2);
26+
* obj->add(index,val);
27+
* int param_2 = obj->count(tot);
28+
*/
29+
30+
json leetcode::qubh::Solve(string input_json_values) {
31+
vector<string> inputArray;
32+
size_t pos = input_json_values.find('\n');
33+
while (pos != string::npos) {
34+
inputArray.push_back(input_json_values.substr(0, pos));
35+
input_json_values = input_json_values.substr(pos + 1);
36+
pos = input_json_values.find('\n');
37+
}
38+
inputArray.push_back(input_json_values);
39+
40+
vector<string> operators = json::parse(inputArray[0]);
41+
vector<vector<json>> op_values = json::parse(inputArray[1]);
42+
vector<int> nums2_array = op_values[0][1].get<vector<int>>();
43+
vector<int> nums1_array = op_values[0][0].get<vector<int>>();
44+
auto obj0 = make_unique<FindSumPairs>(nums1_array, nums2_array);
45+
vector<json> ans = {nullptr};
46+
for (size_t i = 1; i < op_values.size(); ++i) {
47+
if (operators[i] == "add") {
48+
obj0->add(op_values[i][0], op_values[i][1]);
49+
ans.push_back(nullptr);
50+
continue;
51+
}
52+
if (operators[i] == "count") {
53+
ans.push_back(obj0->count(op_values[i][0]));
54+
continue;
55+
}
56+
ans.push_back(nullptr);
57+
}
58+
return ans;
59+
}

problems/problems_1865/Solution.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package problems.problems_1865;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import java.util.*;
5+
import qubhjava.BaseSolution;
6+
7+
8+
class FindSumPairs {
9+
10+
public FindSumPairs(int[] nums1, int[] nums2) {
11+
12+
}
13+
14+
public void add(int index, int val) {
15+
16+
}
17+
18+
public int count(int tot) {
19+
20+
}
21+
}
22+
23+
/**
24+
* Your FindSumPairs object will be instantiated and called as such:
25+
* FindSumPairs obj = new FindSumPairs(nums1, nums2);
26+
* obj.add(index,val);
27+
* int param_2 = obj.count(tot);
28+
*/
29+
30+
public class Solution extends BaseSolution {
31+
32+
33+
@Override
34+
public Object solve(String[] inputJsonValues) {
35+
String[] operators = jsonArrayToStringArray(inputJsonValues[0]);
36+
String[][] opValues = jsonArrayToString2DArray(inputJsonValues[1]);
37+
int[] nums1 = jsonArrayToIntArray(opValues[0][0]);
38+
int[] nums2 = jsonArrayToIntArray(opValues[0][1]);
39+
FindSumPairs obj = new FindSumPairs(nums1, nums2);
40+
List<Object> ans = new ArrayList<>(operators.length);
41+
ans.add(null);
42+
for (int i = 1; i < operators.length; i++) {
43+
if (operators[i].compareTo("add") == 0) {
44+
int index = Integer.parseInt(opValues[i][0]);
45+
int val = Integer.parseInt(opValues[i][1]);
46+
obj.add(index, val);
47+
ans.add(null);
48+
continue;
49+
}
50+
if (operators[i].compareTo("count") == 0) {
51+
int tot = Integer.parseInt(opValues[i][0]);
52+
ans.add(obj.count(tot));
53+
continue;
54+
}
55+
ans.add(null);
56+
}
57+
return JSON.toJSON(ans);
58+
}
59+
}

problems/problems_1865/problem.md

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,51 @@
11
# 1865. Finding Pairs With a Certain Sum [Rating: 1680.82]
22

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>
44

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 &lt;= i &lt; nums1.length</code> and <code>0 &lt;= j &lt; nums2.length</code>).</li>
8+
</ol>
79

8-
Implement the `FindSumPairs` class:
10+
<p>Implement the <code>FindSumPairs</code> class:</p>
911

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>
1317

14-
18+
<p>&nbsp;</p>
19+
<p><strong class="example">Example 1:</strong></p>
1520

16-
**Example 1:**
17-
18-
```
19-
Input
20-
["FindSumPairs", "count", "add", "count", "count", "add", "add", "count"]
21+
<pre>
22+
<strong>Input</strong>
23+
[&quot;FindSumPairs&quot;, &quot;count&quot;, &quot;add&quot;, &quot;count&quot;, &quot;count&quot;, &quot;add&quot;, &quot;add&quot;, &quot;count&quot;]
2124
[[[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>
2326
[null, 8, null, 2, 1, null, null, 11]
2427

25-
Explanation
28+
<strong>Explanation</strong>
2629
FindSumPairs findSumPairs = new FindSumPairs([1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]);
2730
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>]
2932
findSumPairs.count(8); // return 2; pairs (5,2), (5,4) make 3 + 5
3033
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>]
3336
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>&nbsp;</p>
40+
<p><strong>Constraints:</strong></p>
41+
42+
<ul>
43+
<li><code>1 &lt;= nums1.length &lt;= 1000</code></li>
44+
<li><code>1 &lt;= nums2.length &lt;= 10<sup>5</sup></code></li>
45+
<li><code>1 &lt;= nums1[i] &lt;= 10<sup>9</sup></code></li>
46+
<li><code>1 &lt;= nums2[i] &lt;= 10<sup>5</sup></code></li>
47+
<li><code>0 &lt;= index &lt; nums2.length</code></li>
48+
<li><code>1 &lt;= val &lt;= 10<sup>5</sup></code></li>
49+
<li><code>1 &lt;= tot &lt;= 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>

problems/problems_1865/problem_zh.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 1865. 找出和为指定值的下标对 [难度分: 1680.82]
2+
3+
<p>给你两个整数数组 <code>nums1</code> 和 <code>nums2</code> ,请你实现一个支持下述两类查询的数据结构:</p>
4+
5+
<ol>
6+
<li><strong>累加</strong> ,将一个正整数加到 <code>nums2</code> 中指定下标对应元素上。</li>
7+
<li><strong>计数 </strong>,统计满足 <code>nums1[i] + nums2[j]</code> 等于指定值的下标对 <code>(i, j)</code> 数目(<code>0 <= i < nums1.length</code> 且 <code>0 <= j < nums2.length</code>)。</li>
8+
</ol>
9+
10+
<p>实现 <code>FindSumPairs</code> 类:</p>
11+
12+
<ul>
13+
<li><code>FindSumPairs(int[] nums1, int[] nums2)</code> 使用整数数组 <code>nums1</code> 和 <code>nums2</code> 初始化 <code>FindSumPairs</code> 对象。</li>
14+
<li><code>void add(int index, int val)</code> 将 <code>val</code> 加到 <code>nums2[index]</code> 上,即,执行 <code>nums2[index] += val</code> 。</li>
15+
<li><code>int count(int tot)</code> 返回满足 <code>nums1[i] + nums2[j] == tot</code> 的下标对 <code>(i, j)</code> 数目。</li>
16+
</ul>
17+
18+
<p> </p>
19+
20+
<p><strong>示例:</strong></p>
21+
22+
<pre>
23+
<strong>输入:</strong>
24+
["FindSumPairs", "count", "add", "count", "count", "add", "add", "count"]
25+
[[[1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]], [7], [3, 2], [8], [4], [0, 1], [1, 1], [7]]
26+
<strong>输出:</strong>
27+
[null, 8, null, 2, 1, null, null, 11]
28+
29+
<strong>解释:</strong>
30+
FindSumPairs findSumPairs = new FindSumPairs([1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]);
31+
findSumPairs.count(7); // 返回 8 ; 下标对 (2,2), (3,2), (4,2), (2,4), (3,4), (4,4) 满足 2 + 5 = 7 ,下标对 (5,1), (5,5) 满足 3 + 4 = 7
32+
findSumPairs.add(3, 2); // 此时 nums2 = [1,4,5,<em><strong>4</strong></em><code>,5,4</code>]
33+
findSumPairs.count(8); // 返回 2 ;下标对 (5,2), (5,4) 满足 3 + 5 = 8
34+
findSumPairs.count(4); // 返回 1 ;下标对 (5,0) 满足 3 + 1 = 4
35+
findSumPairs.add(0, 1); // 此时 nums2 = [<em><strong><code>2</code></strong></em>,4,5,4<code>,5,4</code>]
36+
findSumPairs.add(1, 1); // 此时 nums2 = [<code>2</code>,<em><strong>5</strong></em>,5,4<code>,5,4</code>]
37+
findSumPairs.count(7); // 返回 11 ;下标对 (2,1), (2,2), (2,4), (3,1), (3,2), (3,4), (4,1), (4,2), (4,4) 满足 2 + 5 = 7 ,下标对 (5,3), (5,5) 满足 3 + 4 = 7
38+
</pre>
39+
40+
<p> </p>
41+
42+
<p><strong>提示:</strong></p>
43+
44+
<ul>
45+
<li><code>1 <= nums1.length <= 1000</code></li>
46+
<li><code>1 <= nums2.length <= 10<sup>5</sup></code></li>
47+
<li><code>1 <= nums1[i] <= 10<sup>9</sup></code></li>
48+
<li><code>1 <= nums2[i] <= 10<sup>5</sup></code></li>
49+
<li><code>0 <= index < nums2.length</code></li>
50+
<li><code>1 <= val <= 10<sup>5</sup></code></li>
51+
<li><code>1 <= tot <= 10<sup>9</sup></code></li>
52+
<li>最多调用 <code>add</code> 和 <code>count</code> 函数各 <code>1000</code> 次</li>
53+
</ul>

problems/problems_1865/solution.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package problem1865
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
"strings"
7+
)
8+
9+
type FindSumPairs struct {
10+
11+
}
12+
13+
14+
func Constructor(nums1 []int, nums2 []int) FindSumPairs {
15+
16+
}
17+
18+
19+
func (this *FindSumPairs) Add(index int, val int) {
20+
21+
}
22+
23+
24+
func (this *FindSumPairs) Count(tot int) int {
25+
26+
}
27+
28+
29+
/**
30+
* Your FindSumPairs object will be instantiated and called as such:
31+
* obj := Constructor(nums1, nums2);
32+
* obj.Add(index,val);
33+
* param_2 := obj.Count(tot);
34+
*/
35+
36+
func Solve(inputJsonValues string) any {
37+
inputValues := strings.Split(inputJsonValues, "\n")
38+
var operators []string
39+
var opValues [][]any
40+
var ans []any
41+
if err := json.Unmarshal([]byte(inputValues[0]), &operators); err != nil {
42+
log.Println(err)
43+
return nil
44+
}
45+
if err := json.Unmarshal([]byte(inputValues[1]), &opValues); err != nil {
46+
log.Println(err)
47+
return nil
48+
}
49+
var nums1Arr []int
50+
if v, ok := opValues[0][0].([]int); ok {
51+
nums1Arr = v
52+
} else {
53+
for _, vi := range opValues[0][0].([]any) {
54+
nums1Arr = append(nums1Arr, int(vi.(float64)))
55+
}
56+
}
57+
var nums2Arr []int
58+
if v, ok := opValues[0][1].([]int); ok {
59+
nums2Arr = v
60+
} else {
61+
for _, vi := range opValues[0][1].([]any) {
62+
nums2Arr = append(nums2Arr, int(vi.(float64)))
63+
}
64+
}
65+
obj := Constructor(nums1Arr, nums2Arr)
66+
ans = append(ans, nil)
67+
for i := 1; i < len(operators); i++ {
68+
var res any
69+
switch operators[i] {
70+
case "add", "Add":
71+
res = nil
72+
obj.Add(int(opValues[i][0].(float64)), int(opValues[i][1].(float64)))
73+
case "count", "Count":
74+
res = obj.Count(int(opValues[i][0].(float64)))
75+
default:
76+
res = nil
77+
}
78+
ans = append(ans, res)
79+
}
80+
81+
82+
return ans
83+
}

0 commit comments

Comments
 (0)