Skip to content

Commit 3fe42a2

Browse files
committed
feat: add solutions to lc problems: No.2335~2338
* No.2335.Minimum Amount of Time to Fill Cups * No.2336.Smallest Number in Infinite Set * No.2337.Move Pieces to Obtain a String * No.2338.Count the Number of Ideal Arrays
1 parent 217ac2e commit 3fe42a2

File tree

59 files changed

+3883
-49
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+3883
-49
lines changed

solution/0500-0599/0561.Array Partition I/Solution.java

-10
This file was deleted.

solution/0500-0599/0561.Array Partition I/README.md solution/0500-0599/0561.Array Partition/README.md

+54-18
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
# [561. 数组拆分 I](https://leetcode.cn/problems/array-partition-i)
1+
# [561. 数组拆分](https://leetcode.cn/problems/array-partition)
22

3-
[English Version](/solution/0500-0599/0561.Array%20Partition%20I/README_EN.md)
3+
[English Version](/solution/0500-0599/0561.Array%20Partition/README_EN.md)
44

55
## 题目描述
66

77
<!-- 这里写题目描述 -->
88

9-
<p>给定长度为 <code>2n</code><strong> </strong>的整数数组 <code>nums</code> ,你的任务是将这些数分成 <code>n</code><strong> </strong>对, 例如 <code>(a<sub>1</sub>, b<sub>1</sub>), (a<sub>2</sub>, b<sub>2</sub>), ..., (a<sub>n</sub>, b<sub>n</sub>)</code> ,使得从 <code>1</code> 到 <code>n</code> 的 <code>min(a<sub>i</sub>, b<sub>i</sub>)</code> 总和最大。</p>
9+
<p>给定长度为&nbsp;<code>2n</code><strong>&nbsp;</strong>的整数数组 <code>nums</code> ,你的任务是将这些数分成&nbsp;<code>n</code><strong> </strong>对, 例如 <code>(a<sub>1</sub>, b<sub>1</sub>), (a<sub>2</sub>, b<sub>2</sub>), ..., (a<sub>n</sub>, b<sub>n</sub>)</code> ,使得从 <code>1</code> 到&nbsp;<code>n</code> 的 <code>min(a<sub>i</sub>, b<sub>i</sub>)</code> 总和最大。</p>
1010

1111
<p>返回该 <strong>最大总和</strong> 。</p>
1212

13-
<p> </p>
13+
<p>&nbsp;</p>
1414

1515
<p><strong>示例 1:</strong></p>
1616

1717
<pre>
1818
<strong>输入:</strong>nums = [1,4,3,2]
1919
<strong>输出:</strong>4
2020
<strong>解释:</strong>所有可能的分法(忽略元素顺序)为:
21-
1. (1, 4), (2, 3) -> min(1, 4) + min(2, 3) = 1 + 2 = 3
22-
2. (1, 3), (2, 4) -> min(1, 3) + min(2, 4) = 1 + 2 = 3
23-
3. (1, 2), (3, 4) -> min(1, 2) + min(3, 4) = 1 + 3 = 4
21+
1. (1, 4), (2, 3) -&gt; min(1, 4) + min(2, 3) = 1 + 2 = 3
22+
2. (1, 3), (2, 4) -&gt; min(1, 3) + min(2, 4) = 1 + 2 = 3
23+
3. (1, 2), (3, 4) -&gt; min(1, 2) + min(3, 4) = 1 + 3 = 4
2424
所以最大总和为 4</pre>
2525

2626
<p><strong>示例 2:</strong></p>
@@ -31,20 +31,23 @@
3131
<strong>解释:</strong>最优的分法为 (2, 1), (2, 5), (6, 6). min(2, 1) + min(2, 5) + min(6, 6) = 1 + 2 + 6 = 9
3232
</pre>
3333

34-
<p> </p>
34+
<p>&nbsp;</p>
3535

3636
<p><strong>提示:</strong></p>
3737

3838
<ul>
39-
<li><code>1 <= n <= 10<sup>4</sup></code></li>
39+
<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
4040
<li><code>nums.length == 2 * n</code></li>
41-
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
41+
<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
4242
</ul>
4343

44+
4445
## 解法
4546

4647
<!-- 这里可写通用的实现逻辑 -->
4748

49+
**方法一:排序**
50+
4851
先排序,然后求相邻的两个元素的最小值,得到的总和即为结果。
4952

5053
<!-- tabs:start -->
@@ -67,15 +70,42 @@ class Solution:
6770
class Solution {
6871
public int arrayPairSum(int[] nums) {
6972
Arrays.sort(nums);
70-
int res = 0;
71-
for (int i = 0, n = nums.length; i < n; i += 2) {
72-
res += nums[i];
73+
int ans = 0;
74+
for (int i = 0; i < nums.length; i += 2) {
75+
ans += nums[i];
7376
}
74-
return res;
77+
return ans;
7578
}
7679
}
7780
```
7881

82+
### **C++**
83+
84+
```cpp
85+
class Solution {
86+
public:
87+
int arrayPairSum(vector<int>& nums) {
88+
sort(nums.begin(), nums.end());
89+
int ans = 0;
90+
for (int i = 0; i < nums.size(); i += 2) ans += nums[i];
91+
return ans;
92+
}
93+
};
94+
```
95+
96+
### **Go**
97+
98+
```go
99+
func arrayPairSum(nums []int) int {
100+
sort.Ints(nums)
101+
ans := 0
102+
for i := 0; i < len(nums); i += 2 {
103+
ans += nums[i]
104+
}
105+
return ans
106+
}
107+
```
108+
79109
### **JavaScript**
80110

81111
```js
@@ -85,11 +115,11 @@ class Solution {
85115
*/
86116
var arrayPairSum = function (nums) {
87117
nums.sort((a, b) => a - b);
88-
let res = 0;
89-
for (let i = 0, n = nums.length; i < n; i += 2) {
90-
res += nums[i];
118+
let ans = 0;
119+
for (let i = 0; i < nums.length; i += 2) {
120+
ans += nums[i];
91121
}
92-
return res;
122+
return ans;
93123
};
94124
```
95125

@@ -111,6 +141,12 @@ impl Solution {
111141
}
112142
```
113143

144+
### **TypeScript**
145+
146+
```ts
147+
148+
```
149+
114150
### **...**
115151

116152
```

solution/0500-0599/0561.Array Partition I/README_EN.md solution/0500-0599/0561.Array Partition/README_EN.md

+44-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# [561. Array Partition I](https://leetcode.com/problems/array-partition-i)
1+
# [561. Array Partition](https://leetcode.com/problems/array-partition)
22

3-
[中文文档](/solution/0500-0599/0561.Array%20Partition%20I/README.md)
3+
[中文文档](/solution/0500-0599/0561.Array%20Partition/README.md)
44

55
## Description
66

@@ -35,6 +35,7 @@ So the maximum possible sum is 4.</pre>
3535
<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
3636
</ul>
3737

38+
3839
## Solutions
3940

4041
<!-- tabs:start -->
@@ -53,12 +54,39 @@ class Solution:
5354
class Solution {
5455
public int arrayPairSum(int[] nums) {
5556
Arrays.sort(nums);
56-
int res = 0;
57-
for (int i = 0, n = nums.length; i < n; i += 2) {
58-
res += nums[i];
57+
int ans = 0;
58+
for (int i = 0; i < nums.length; i += 2) {
59+
ans += nums[i];
5960
}
60-
return res;
61+
return ans;
62+
}
63+
}
64+
```
65+
66+
### **C++**
67+
68+
```cpp
69+
class Solution {
70+
public:
71+
int arrayPairSum(vector<int>& nums) {
72+
sort(nums.begin(), nums.end());
73+
int ans = 0;
74+
for (int i = 0; i < nums.size(); i += 2) ans += nums[i];
75+
return ans;
6176
}
77+
};
78+
```
79+
80+
### **Go**
81+
82+
```go
83+
func arrayPairSum(nums []int) int {
84+
sort.Ints(nums)
85+
ans := 0
86+
for i := 0; i < len(nums); i += 2 {
87+
ans += nums[i]
88+
}
89+
return ans
6290
}
6391
```
6492

@@ -71,11 +99,11 @@ class Solution {
7199
*/
72100
var arrayPairSum = function (nums) {
73101
nums.sort((a, b) => a - b);
74-
let res = 0;
75-
for (let i = 0, n = nums.length; i < n; i += 2) {
76-
res += nums[i];
102+
let ans = 0;
103+
for (let i = 0; i < nums.length; i += 2) {
104+
ans += nums[i];
77105
}
78-
return res;
106+
return ans;
79107
};
80108
```
81109

@@ -97,6 +125,12 @@ impl Solution {
97125
}
98126
```
99127

128+
### **TypeScript**
129+
130+
```ts
131+
132+
```
133+
100134
### **...**
101135

102136
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public:
3+
int arrayPairSum(vector<int>& nums) {
4+
sort(nums.begin(), nums.end());
5+
int ans = 0;
6+
for (int i = 0; i < nums.size(); i += 2) ans += nums[i];
7+
return ans;
8+
}
9+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func arrayPairSum(nums []int) int {
2+
sort.Ints(nums)
3+
ans := 0
4+
for i := 0; i < len(nums); i += 2 {
5+
ans += nums[i]
6+
}
7+
return ans
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public int arrayPairSum(int[] nums) {
3+
Arrays.sort(nums);
4+
int ans = 0;
5+
for (int i = 0; i < nums.length; i += 2) {
6+
ans += nums[i];
7+
}
8+
return ans;
9+
}
10+
}

solution/0500-0599/0561.Array Partition I/Solution.js solution/0500-0599/0561.Array Partition/Solution.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
*/
55
var arrayPairSum = function (nums) {
66
nums.sort((a, b) => a - b);
7-
let res = 0;
8-
for (let i = 0, n = nums.length; i < n; i += 2) {
9-
res += nums[i];
7+
let ans = 0;
8+
for (let i = 0; i < nums.length; i += 2) {
9+
ans += nums[i];
1010
}
11-
return res;
11+
return ans;
1212
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# [2330. Valid Palindrome IV](https://leetcode.cn/problems/valid-palindrome-iv)
2+
3+
[English Version](/solution/2300-2399/2330.Valid%20Palindrome%20IV/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>You are given a <strong>0-indexed</strong> string <code>s</code> consisting of only lowercase English letters. In one operation, you can change <strong>any</strong> character of <code>s</code> to any <strong>other</strong> character.</p>
10+
11+
<p>Return <code>true</code><em> if you can make </em><code>s</code><em> a palindrome after performing <strong>exactly</strong> one or two operations, or return </em><code>false</code><em> otherwise.</em></p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong>Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> s = &quot;abcdba&quot;
18+
<strong>Output:</strong> true
19+
<strong>Explanation:</strong> One way to make s a palindrome using 1 operation is:
20+
- Change s[2] to &#39;d&#39;. Now, s = &quot;abddba&quot;.
21+
One operation could be performed to make s a palindrome so return true.
22+
</pre>
23+
24+
<p><strong>Example 2:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> s = &quot;aa&quot;
28+
<strong>Output:</strong> true
29+
<strong>Explanation:</strong> One way to make s a palindrome using 2 operations is:
30+
- Change s[0] to &#39;b&#39;. Now, s = &quot;ba&quot;.
31+
- Change s[1] to &#39;b&#39;. Now, s = &quot;bb&quot;.
32+
Two operations could be performed to make s a palindrome so return true.
33+
</pre>
34+
35+
<p><strong>Example 3:</strong></p>
36+
37+
<pre>
38+
<strong>Input:</strong> s = &quot;abcdef&quot;
39+
<strong>Output:</strong> false
40+
<strong>Explanation:</strong> It is not possible to make s a palindrome using one or two operations so return false.
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
<p><strong>Constraints:</strong></p>
45+
46+
<ul>
47+
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
48+
<li><code>s</code> consists only of lowercase English letters.</li>
49+
</ul>
50+
51+
## 解法
52+
53+
<!-- 这里可写通用的实现逻辑 -->
54+
55+
<!-- tabs:start -->
56+
57+
### **Python3**
58+
59+
<!-- 这里可写当前语言的特殊实现逻辑 -->
60+
61+
```python
62+
63+
```
64+
65+
### **Java**
66+
67+
<!-- 这里可写当前语言的特殊实现逻辑 -->
68+
69+
```java
70+
71+
```
72+
73+
### **TypeScript**
74+
75+
```ts
76+
77+
```
78+
79+
### **...**
80+
81+
```
82+
83+
```
84+
85+
<!-- tabs:end -->

0 commit comments

Comments
 (0)