Skip to content

Commit 203fa91

Browse files
committed
feat: add solutions to lc problem: No.2341
No.2341.Maximum Number of Pairs in Array
1 parent 7984256 commit 203fa91

File tree

7 files changed

+101
-48
lines changed

7 files changed

+101
-48
lines changed

solution/2300-2399/2341.Maximum Number of Pairs in Array/README.md

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,15 @@ nums[0] 和 nums[1] 形成一个数对,并从 nums 中移除,nums = [2] 。
5858

5959
**方法一:计数**
6060

61-
我们可以统计数组 `nums` 中每个数字出现的次数,记录在数组 `cnt` 中。然后遍历数组 `cnt`,对于每个数字 $v$,如果 $v$ 出现的次数大于等于 $2$,则可以从数组中选出两个 $v$ 形成一个数对,此时我们将 $v$ 出现的次数除以 $2$,即可得到可以形成的数对数目,然后将这个数对数目加到答案中
61+
我们可以统计数组 `nums` 中每个数字 $x$ 出现的次数,记录在哈希表或数组 `cnt`
6262

63-
最终剩余的数字个数为数组 `nums` 的长度减去可以形成的数对数目乘以 $2$
63+
然后遍历 `cnt`,对于每个数字 $x$,如果 $x$ 出现的次数 $v$ 大于 $1$,则可以从数组中选出两个 $x$ 形成一个数对,我们将 $v$ 除以 $2$ 向下取整,即可得到当前数字 $x$ 可以形成的数对数目,然后我们累加这个数目到变量 $s$ 中
6464

65-
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为数组 `nums` 的长度,而 $C$ 为数组 `nums` 中数字的范围。本题中 $C = 101$。
65+
最后剩余的个数为数组 `nums` 的长度减去可以形成的数对数目乘以 $2$,即 $n - s \times 2$。
66+
67+
答案为 $[s, n - s \times 2]$。
68+
69+
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为数组 `nums` 的长度;而 $C$ 为数组 `nums` 中数字的范围,本题中 $C = 101$。
6670

6771
<!-- tabs:start -->
6872

@@ -86,8 +90,8 @@ class Solution:
8690
class Solution {
8791
public int[] numberOfPairs(int[] nums) {
8892
int[] cnt = new int[101];
89-
for (int v : nums) {
90-
++cnt[v];
93+
for (int x : nums) {
94+
++cnt[x];
9195
}
9296
int s = 0;
9397
for (int v : cnt) {
@@ -105,8 +109,8 @@ class Solution {
105109
public:
106110
vector<int> numberOfPairs(vector<int>& nums) {
107111
vector<int> cnt(101);
108-
for (int& v : nums) {
109-
++cnt[v];
112+
for (int& x : nums) {
113+
++cnt[x];
110114
}
111115
int s = 0;
112116
for (int& v : cnt) {
@@ -121,15 +125,15 @@ public:
121125
122126
```go
123127
func numberOfPairs(nums []int) []int {
124-
cnt := make([]int, 101)
125-
for _, v := range nums {
126-
cnt[v]++
127-
}
128-
s := 0
129-
for _, v := range cnt {
130-
s += v / 2
131-
}
132-
return []int{s, len(nums) - s * 2}
128+
cnt := [101]int{}
129+
for _, x := range nums {
130+
cnt[x]++
131+
}
132+
s := 0
133+
for _, v := range cnt {
134+
s += v / 2
135+
}
136+
return []int{s, len(nums) - s*2}
133137
}
134138
```
135139

@@ -198,14 +202,32 @@ int *numberOfPairs(int *nums, int numsSize, int *returnSize) {
198202
*/
199203
var numberOfPairs = function (nums) {
200204
const cnt = new Array(101).fill(0);
201-
for (const v of nums) {
202-
++cnt[v];
205+
for (const x of nums) {
206+
++cnt[x];
203207
}
204208
const s = cnt.reduce((a, b) => a + (b >> 1), 0);
205209
return [s, nums.length - s * 2];
206210
};
207211
```
208212

213+
### **C#**
214+
215+
```cs
216+
public class Solution {
217+
public int[] NumberOfPairs(int[] nums) {
218+
int[] cnt = new int[101];
219+
foreach(int x in nums) {
220+
++cnt[x];
221+
}
222+
int s = 0;
223+
foreach(int v in cnt) {
224+
s += v / 2;
225+
}
226+
return new int[] {s, nums.Length - s * 2};
227+
}
228+
}
229+
```
230+
209231
### **...**
210232

211233
```

solution/2300-2399/2341.Maximum Number of Pairs in Array/README_EN.md

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ class Solution:
7373
class Solution {
7474
public int[] numberOfPairs(int[] nums) {
7575
int[] cnt = new int[101];
76-
for (int v : nums) {
77-
++cnt[v];
76+
for (int x : nums) {
77+
++cnt[x];
7878
}
7979
int s = 0;
8080
for (int v : cnt) {
@@ -92,8 +92,8 @@ class Solution {
9292
public:
9393
vector<int> numberOfPairs(vector<int>& nums) {
9494
vector<int> cnt(101);
95-
for (int& v : nums) {
96-
++cnt[v];
95+
for (int& x : nums) {
96+
++cnt[x];
9797
}
9898
int s = 0;
9999
for (int& v : cnt) {
@@ -108,15 +108,15 @@ public:
108108
109109
```go
110110
func numberOfPairs(nums []int) []int {
111-
cnt := make([]int, 101)
112-
for _, v := range nums {
113-
cnt[v]++
114-
}
115-
s := 0
116-
for _, v := range cnt {
117-
s += v / 2
118-
}
119-
return []int{s, len(nums) - s * 2}
111+
cnt := [101]int{}
112+
for _, x := range nums {
113+
cnt[x]++
114+
}
115+
s := 0
116+
for _, v := range cnt {
117+
s += v / 2
118+
}
119+
return []int{s, len(nums) - s*2}
120120
}
121121
```
122122

@@ -185,14 +185,32 @@ int *numberOfPairs(int *nums, int numsSize, int *returnSize) {
185185
*/
186186
var numberOfPairs = function (nums) {
187187
const cnt = new Array(101).fill(0);
188-
for (const v of nums) {
189-
++cnt[v];
188+
for (const x of nums) {
189+
++cnt[x];
190190
}
191191
const s = cnt.reduce((a, b) => a + (b >> 1), 0);
192192
return [s, nums.length - s * 2];
193193
};
194194
```
195195

196+
### **C#**
197+
198+
```cs
199+
public class Solution {
200+
public int[] NumberOfPairs(int[] nums) {
201+
int[] cnt = new int[101];
202+
foreach(int x in nums) {
203+
++cnt[x];
204+
}
205+
int s = 0;
206+
foreach(int v in cnt) {
207+
s += v / 2;
208+
}
209+
return new int[] {s, nums.Length - s * 2};
210+
}
211+
}
212+
```
213+
196214
### **...**
197215

198216
```

solution/2300-2399/2341.Maximum Number of Pairs in Array/Solution.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ class Solution {
22
public:
33
vector<int> numberOfPairs(vector<int>& nums) {
44
vector<int> cnt(101);
5-
for (int& v : nums) {
6-
++cnt[v];
5+
for (int& x : nums) {
6+
++cnt[x];
77
}
88
int s = 0;
99
for (int& v : cnt) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class Solution {
2+
public int[] NumberOfPairs(int[] nums) {
3+
int[] cnt = new int[101];
4+
foreach(int x in nums) {
5+
++cnt[x];
6+
}
7+
int s = 0;
8+
foreach(int v in cnt) {
9+
s += v / 2;
10+
}
11+
return new int[] {s, nums.Length - s * 2};
12+
}
13+
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
func numberOfPairs(nums []int) []int {
2-
cnt := make([]int, 101)
3-
for _, v := range nums {
4-
cnt[v]++
5-
}
6-
s := 0
7-
for _, v := range cnt {
8-
s += v / 2
9-
}
10-
return []int{s, len(nums) - s * 2}
2+
cnt := [101]int{}
3+
for _, x := range nums {
4+
cnt[x]++
5+
}
6+
s := 0
7+
for _, v := range cnt {
8+
s += v / 2
9+
}
10+
return []int{s, len(nums) - s*2}
1111
}

solution/2300-2399/2341.Maximum Number of Pairs in Array/Solution.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution {
22
public int[] numberOfPairs(int[] nums) {
33
int[] cnt = new int[101];
4-
for (int v : nums) {
5-
++cnt[v];
4+
for (int x : nums) {
5+
++cnt[x];
66
}
77
int s = 0;
88
for (int v : cnt) {

solution/2300-2399/2341.Maximum Number of Pairs in Array/Solution.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*/
55
var numberOfPairs = function (nums) {
66
const cnt = new Array(101).fill(0);
7-
for (const v of nums) {
8-
++cnt[v];
7+
for (const x of nums) {
8+
++cnt[x];
99
}
1010
const s = cnt.reduce((a, b) => a + (b >> 1), 0);
1111
return [s, nums.length - s * 2];

0 commit comments

Comments
 (0)