Skip to content

Commit e374d74

Browse files
committed
feat: add solutions to lc problems: No.2341~2344
* No.2341.Maximum Number of Pairs in Array * No.2342.Max Sum of a Pair With Equal Sum of Digits * No.2343.Query Kth Smallest Trimmed Number * No.2344.Minimum Deletions to Make Array Divisible
1 parent 05386b8 commit e374d74

File tree

28 files changed

+1447
-1
lines changed

28 files changed

+1447
-1
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# [2341. 数组能形成多少数对](https://leetcode.cn/problems/maximum-number-of-pairs-in-array)
2+
3+
[English Version](/solution/2300-2399/2341.Maximum%20Number%20of%20Pairs%20in%20Array/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。在一步操作中,你可以执行以下步骤:</p>
10+
11+
<ul>
12+
<li>从 <code>nums</code> 选出 <strong>两个</strong> <strong>相等的</strong> 整数</li>
13+
<li>从 <code>nums</code> 中移除这两个整数,形成一个 <strong>数对</strong></li>
14+
</ul>
15+
16+
<p>请你在 <code>nums</code> 上多次执行此操作直到无法继续执行。</p>
17+
18+
<p>返回一个下标从 <strong>0</strong> 开始、长度为 <code>2</code> 的整数数组 <code>answer</code> 作为答案,其中<em> </em><code>answer[0]</code><em> </em>是形成的数对数目,<code>answer[1]</code> 是对 <code>nums</code> 尽可能执行上述操作后剩下的整数数目。</p>
19+
20+
<p>&nbsp;</p>
21+
22+
<p><strong>示例 1:</strong></p>
23+
24+
<pre><strong>输入:</strong>nums = [1,3,2,1,3,2,2]
25+
<strong>输出:</strong>[3,1]
26+
<strong>解释:</strong>
27+
nums[0] 和 nums[3] 形成一个数对,并从 nums 中移除,nums = [3,2,3,2,2] 。
28+
nums[0] 和 nums[2] 形成一个数对,并从 nums 中移除,nums = [2,2,2] 。
29+
nums[0] 和 nums[1] 形成一个数对,并从 nums 中移除,nums = [2] 。
30+
无法形成更多数对。总共形成 3 个数对,nums 中剩下 1 个数字。</pre>
31+
32+
<p><strong>示例 2:</strong></p>
33+
34+
<pre><strong>输入:</strong>nums = [1,1]
35+
<strong>输出:</strong>[1,0]
36+
<strong>解释:</strong>nums[0] 和 nums[1] 形成一个数对,并从 nums 中移除,nums = [] 。
37+
无法形成更多数对。总共形成 1 个数对,nums 中剩下 0 个数字。</pre>
38+
39+
<p><strong>示例 3:</strong></p>
40+
41+
<pre><strong>输入:</strong>nums = [0]
42+
<strong>输出:</strong>[0,1]
43+
<strong>解释:</strong>无法形成数对,nums 中剩下 1 个数字。
44+
</pre>
45+
46+
<p>&nbsp;</p>
47+
48+
<p><strong>提示:</strong></p>
49+
50+
<ul>
51+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
52+
<li><code>0 &lt;= nums[i] &lt;= 100</code></li>
53+
</ul>
54+
55+
## 解法
56+
57+
<!-- 这里可写通用的实现逻辑 -->
58+
59+
<!-- tabs:start -->
60+
61+
### **Python3**
62+
63+
<!-- 这里可写当前语言的特殊实现逻辑 -->
64+
65+
```python
66+
class Solution:
67+
def numberOfPairs(self, nums: List[int]) -> List[int]:
68+
cnt = Counter(nums)
69+
s = sum(v // 2 for v in cnt.values())
70+
return [s, len(nums) - s * 2]
71+
```
72+
73+
### **Java**
74+
75+
<!-- 这里可写当前语言的特殊实现逻辑 -->
76+
77+
```java
78+
class Solution {
79+
public int[] numberOfPairs(int[] nums) {
80+
int[] cnt = new int[101];
81+
for (int v : nums) {
82+
++cnt[v];
83+
}
84+
int s = 0;
85+
for (int v : cnt) {
86+
s += v / 2;
87+
}
88+
return new int[]{s, nums.length - s * 2};
89+
}
90+
}
91+
```
92+
93+
### **C++**
94+
95+
```cpp
96+
class Solution {
97+
public:
98+
vector<int> numberOfPairs(vector<int>& nums) {
99+
vector<int> cnt(101);
100+
for (int v : nums) ++cnt[v];
101+
int s = 0;
102+
for (int v : cnt) s += v / 2;
103+
vector<int> ans(2);
104+
ans[0] = s;
105+
ans[1] = nums.size() - s * 2;
106+
return ans;
107+
}
108+
};
109+
```
110+
111+
### **Go**
112+
113+
```go
114+
func numberOfPairs(nums []int) []int {
115+
cnt := make([]int, 101)
116+
for _, v := range nums {
117+
cnt[v]++
118+
}
119+
s := 0
120+
for _, v := range cnt {
121+
s += v / 2
122+
}
123+
return []int{s, len(nums) - s * 2}
124+
}
125+
```
126+
127+
### **TypeScript**
128+
129+
```ts
130+
131+
```
132+
133+
### **...**
134+
135+
```
136+
137+
```
138+
139+
<!-- tabs:end -->
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# [2341. Maximum Number of Pairs in Array](https://leetcode.com/problems/maximum-number-of-pairs-in-array)
2+
3+
[中文文档](/solution/2300-2399/2341.Maximum%20Number%20of%20Pairs%20in%20Array/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation, you may do the following:</p>
8+
9+
<ul>
10+
<li>Choose <strong>two</strong> integers in <code>nums</code> that are <strong>equal</strong>.</li>
11+
<li>Remove both integers from <code>nums</code>, forming a <strong>pair</strong>.</li>
12+
</ul>
13+
14+
<p>The operation is done on <code>nums</code> as many times as possible.</p>
15+
16+
<p>Return <em>a <strong>0-indexed</strong> integer array </em><code>answer</code><em> of size </em><code>2</code><em> where </em><code>answer[0]</code><em> is the number of pairs that are formed and </em><code>answer[1]</code><em> is the number of leftover integers in </em><code>nums</code><em> after doing the operation as many times as possible</em>.</p>
17+
18+
<p>&nbsp;</p>
19+
<p><strong>Example 1:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> nums = [1,3,2,1,3,2,2]
23+
<strong>Output:</strong> [3,1]
24+
<strong>Explanation:</strong>
25+
Form a pair with nums[0] and nums[3] and remove them from nums. Now, nums = [3,2,3,2,2].
26+
Form a pair with nums[0] and nums[2] and remove them from nums. Now, nums = [2,2,2].
27+
Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums = [2].
28+
No more pairs can be formed. A total of 3 pairs have been formed, and there is 1 number leftover in nums.
29+
</pre>
30+
31+
<p><strong>Example 2:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong> nums = [1,1]
35+
<strong>Output:</strong> [1,0]
36+
<strong>Explanation:</strong> Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums = [].
37+
No more pairs can be formed. A total of 1 pair has been formed, and there are 0 numbers leftover in nums.
38+
</pre>
39+
40+
<p><strong>Example 3:</strong></p>
41+
42+
<pre>
43+
<strong>Input:</strong> nums = [0]
44+
<strong>Output:</strong> [0,1]
45+
<strong>Explanation:</strong> No pairs can be formed, and there is 1 number leftover in nums.
46+
</pre>
47+
48+
<p>&nbsp;</p>
49+
<p><strong>Constraints:</strong></p>
50+
51+
<ul>
52+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
53+
<li><code>0 &lt;= nums[i] &lt;= 100</code></li>
54+
</ul>
55+
56+
## Solutions
57+
58+
<!-- tabs:start -->
59+
60+
### **Python3**
61+
62+
```python
63+
class Solution:
64+
def numberOfPairs(self, nums: List[int]) -> List[int]:
65+
cnt = Counter(nums)
66+
s = sum(v // 2 for v in cnt.values())
67+
return [s, len(nums) - s * 2]
68+
```
69+
70+
### **Java**
71+
72+
```java
73+
class Solution {
74+
public int[] numberOfPairs(int[] nums) {
75+
int[] cnt = new int[101];
76+
for (int v : nums) {
77+
++cnt[v];
78+
}
79+
int s = 0;
80+
for (int v : cnt) {
81+
s += v / 2;
82+
}
83+
return new int[]{s, nums.length - s * 2};
84+
}
85+
}
86+
```
87+
88+
### **C++**
89+
90+
```cpp
91+
class Solution {
92+
public:
93+
vector<int> numberOfPairs(vector<int>& nums) {
94+
vector<int> cnt(101);
95+
for (int v : nums) ++cnt[v];
96+
int s = 0;
97+
for (int v : cnt) s += v / 2;
98+
vector<int> ans(2);
99+
ans[0] = s;
100+
ans[1] = nums.size() - s * 2;
101+
return ans;
102+
}
103+
};
104+
```
105+
106+
### **Go**
107+
108+
```go
109+
func numberOfPairs(nums []int) []int {
110+
cnt := make([]int, 101)
111+
for _, v := range nums {
112+
cnt[v]++
113+
}
114+
s := 0
115+
for _, v := range cnt {
116+
s += v / 2
117+
}
118+
return []int{s, len(nums) - s * 2}
119+
}
120+
```
121+
122+
### **TypeScript**
123+
124+
```ts
125+
126+
```
127+
128+
### **...**
129+
130+
```
131+
132+
```
133+
134+
<!-- tabs:end -->
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
vector<int> numberOfPairs(vector<int>& nums) {
4+
vector<int> cnt(101);
5+
for (int v : nums) ++cnt[v];
6+
int s = 0;
7+
for (int v : cnt) s += v / 2;
8+
vector<int> ans(2);
9+
ans[0] = s;
10+
ans[1] = nums.size() - s * 2;
11+
return ans;
12+
}
13+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
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}
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int[] numberOfPairs(int[] nums) {
3+
int[] cnt = new int[101];
4+
for (int v : nums) {
5+
++cnt[v];
6+
}
7+
int s = 0;
8+
for (int v : cnt) {
9+
s += v / 2;
10+
}
11+
return new int[]{s, nums.length - s * 2};
12+
}
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def numberOfPairs(self, nums: List[int]) -> List[int]:
3+
cnt = Counter(nums)
4+
s = sum(v // 2 for v in cnt.values())
5+
return [s, len(nums) - s * 2]

0 commit comments

Comments
 (0)