Skip to content

Commit e761584

Browse files
authored
feat: update solutions to lc problems: No.2340~2347 (#3838)
1 parent 3b4a836 commit e761584

File tree

13 files changed

+70
-164
lines changed

13 files changed

+70
-164
lines changed

.github/workflows/pr-add-label.yml

-28
This file was deleted.

solution/2300-2399/2340.Minimum Adjacent Swaps to Make a Valid Array/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ tags:
7171

7272
### 方法一:维护最值下标 + 分类讨论
7373

74-
我们可以用下标 $i$ 和 $j$ 分别记录数组 `nums` 第一个最小值和最后一个最大值的下标,遍历数组 `nums`,更新 $i$ 和 $j$ 的值。
74+
我们可以用下标 $i$ 和 $j$ 分别记录数组 $\textit{nums}$ 第一个最小值和最后一个最大值的下标,遍历数组 $\textit{nums}$,更新 $i$ 和 $j$ 的值。
7575

7676
接下来,我们需要考虑交换的次数。
7777

78-
- 如果 $i = j$,说明数组 `nums` 已经是有效数组,不需要交换,返回 $0$;
79-
- 如果 $i < j$,说明数组 `nums` 中最小值在最大值的左边,需要交换 $i + n - 1 - j$ 次,其中 $n$ 为数组 `nums` 的长度;
80-
- 如果 $i > j$,说明数组 `nums` 中最小值在最大值的右边,需要交换 $i + n - 1 - j - 1$ 次。
78+
- 如果 $i = j$,说明数组 $\textit{nums}$ 已经是有效数组,不需要交换,返回 $0$;
79+
- 如果 $i < j$,说明数组 $\textit{nums}$ 中最小值在最大值的左边,需要交换 $i + n - 1 - j$ 次,其中 $n$ 为数组 $\textit{nums}$ 的长度;
80+
- 如果 $i > j$,说明数组 $\textit{nums}$ 中最小值在最大值的右边,需要交换 $i + n - 1 - j - 1$ 次。
8181

82-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
82+
时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$
8383

8484
<!-- tabs:start -->
8585

solution/2300-2399/2340.Minimum Adjacent Swaps to Make a Valid Array/README_EN.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,17 @@ It can be shown that 6 swaps is the minimum swaps required to make a valid array
6868

6969
<!-- solution:start -->
7070

71-
### Solution 1
71+
### Solution 1: Maintain Index of Extremes + Case Analysis
72+
73+
We can use indices $i$ and $j$ to record the index of the first minimum value and the last maximum value in the array $\textit{nums}$, respectively. Traverse the array $\textit{nums}$ to update the values of $i$ and $j$.
74+
75+
Next, we need to consider the number of swaps.
76+
77+
- If $i = j$, it means the array $\textit{nums}$ is already a valid array, and no swaps are needed. Return $0$.
78+
- If $i < j$, it means the minimum value in the array $\textit{nums}$ is to the left of the maximum value. The number of swaps needed is $i + n - 1 - j$, where $n$ is the length of the array $\textit{nums}$.
79+
- If $i > j$, it means the minimum value in the array $\textit{nums}$ is to the right of the maximum value. The number of swaps needed is $i + n - 1 - j - 1$.
80+
81+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.
7282

7383
<!-- tabs:start -->
7484

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ nums[0] 和 nums[1] 形成一个数对,并从 nums 中移除,nums = [2] 。
7474

7575
### 方法一:计数
7676

77-
我们可以统计数组 `nums` 中每个数字 $x$ 出现的次数,记录在哈希表或数组 `cnt` 中。
77+
我们可以统计数组 $\textit{nums}$ 中每个数字 $x$ 出现的次数,记录在哈希表或数组 $\textit{cnt}$ 中。
7878

79-
然后遍历 `cnt`,对于每个数字 $x$,如果 $x$ 出现的次数 $v$ 大于 $1$,则可以从数组中选出两个 $x$ 形成一个数对,我们将 $v$ 除以 $2$ 向下取整,即可得到当前数字 $x$ 可以形成的数对数目,然后我们累加这个数目到变量 $s$ 中。
79+
然后遍历 $\textit{cnt}$,对于每个数字 $x$,如果 $x$ 出现的次数 $v$ 大于 $1$,则可以从数组中选出两个 $x$ 形成一个数对,我们将 $v$ 除以 $2$ 向下取整,即可得到当前数字 $x$ 可以形成的数对数目,然后我们累加这个数目到变量 $s$ 中。
8080

81-
最后剩余的个数为数组 `nums` 的长度减去可以形成的数对数目乘以 $2$,即 $n - s \times 2$。
81+
最后剩余的个数为数组 $\textit{nums}$ 的长度减去可以形成的数对数目乘以 $2$,即 $n - s \times 2$。
8282

8383
答案为 $[s, n - s \times 2]$。
8484

85-
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为数组 `nums` 的长度;而 $C$ 为数组 `nums` 中数字的范围,本题中 $C = 101$。
85+
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为数组 $\textit{nums}$ 的长度;而 $C$ 为数组 $\textit{nums}$ 中数字的范围,本题中 $C = 101$。
8686

8787
<!-- tabs:start -->
8888

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,17 @@ No more pairs can be formed. A total of 1 pair has been formed, and there are 0
7575

7676
<!-- solution:start -->
7777

78-
### Solution 1
78+
### Solution 1: Counting
79+
80+
We can count the occurrences of each number $x$ in the array $\textit{nums}$ and record them in a hash table or array $\textit{cnt}$.
81+
82+
Then, we traverse $\textit{cnt}$. For each number $x$, if the occurrence count $v$ of $x$ is greater than $1$, we can select two $x$'s from the array to form a pair. We divide $v$ by $2$ and take the floor value to get the number of pairs that can be formed by the current number $x$. We then add this number to the variable $s$.
83+
84+
The remaining count is the length of the array $\textit{nums}$ minus the number of pairs formed multiplied by $2$, i.e., $n - s \times 2$.
85+
86+
The answer is $[s, n - s \times 2]$.
87+
88+
The time complexity is $O(n)$, and the space complexity is $O(C)$. Here, $n$ is the length of the array $\textit{nums}$, and $C$ is the range of numbers in the array $\textit{nums}$, which is $101$ in this problem.
7989

8090
<!-- tabs:start -->
8191

solution/2300-2399/2343.Query Kth Smallest Trimmed Number/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ tags:
9898

9999
根据题意,我们可以模拟裁剪过程,然后对裁剪后的字符串进行排序,最后根据下标找到对应的数字即可。
100100

101-
时间复杂度 $O(m \times \ n \times \log n \times s)$,空间复杂度 $O(n)$。其中 $m$ 和 $n$ 分别为 `nums``queries` 的长度,而 $s$ 为 $nums[i]$ 字符串的长度。
101+
时间复杂度 $O(m \times \ n \times \log n \times s)$,空间复杂度 $O(n)$。其中 $m$ 和 $n$ 分别为 $\textit{nums}$$\textit{queries}$ 的长度,而 $s$ 为 $\textit{nums}[i]$ 字符串的长度。
102102

103103
<!-- tabs:start -->
104104

solution/2300-2399/2343.Query Kth Smallest Trimmed Number/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ tags:
9191

9292
<!-- solution:start -->
9393

94-
### Solution 1
94+
### Solution 1: Simulation
95+
96+
According to the problem description, we can simulate the cropping process, then sort the cropped strings, and finally find the corresponding number based on the index.
97+
98+
The time complexity is $O(m \times n \times \log n \times s)$, and the space complexity is $O(n)$. Here, $m$ and $n$ are the lengths of $\textit{nums}$ and $\textit{queries}$ respectively, and $s$ is the length of the string $\textit{nums}[i]$.
9599

96100
<!-- tabs:start -->
97101

solution/2300-2399/2345.Finding the Number of Visible Mountains/README.md

+3-44
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,20 @@ class Solution {
100100
public int visibleMountains(int[][] peaks) {
101101
int n = peaks.length;
102102
int[][] arr = new int[n][2];
103-
Map<String, Integer> cnt = new HashMap<>();
104103
for (int i = 0; i < n; ++i) {
105104
int x = peaks[i][0], y = peaks[i][1];
106105
arr[i] = new int[] {x - y, x + y};
107-
cnt.merge((x - y) + "" + (x + y), 1, Integer::sum);
108106
}
109107
Arrays.sort(arr, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
110108
int ans = 0;
111109
int cur = Integer.MIN_VALUE;
112-
for (int[] e : arr) {
113-
int l = e[0], r = e[1];
110+
for (int i = 0; i < n; ++i) {
111+
int l = arr[i][0], r = arr[i][1];
114112
if (r <= cur) {
115113
continue;
116114
}
117115
cur = r;
118-
if (cnt.get(l + "" + r) == 1) {
116+
if (!(i < n - 1 && arr[i][0] == arr[i + 1][0] && arr[i][1] == arr[i + 1][1])) {
119117
++ans;
120118
}
121119
}
@@ -182,43 +180,4 @@ func visibleMountains(peaks [][]int) (ans int) {
182180

183181
<!-- solution:end -->
184182

185-
<!-- solution:start -->
186-
187-
### 方法二
188-
189-
<!-- tabs:start -->
190-
191-
#### Java
192-
193-
```java
194-
class Solution {
195-
public int visibleMountains(int[][] peaks) {
196-
int n = peaks.length;
197-
int[][] arr = new int[n][2];
198-
for (int i = 0; i < n; ++i) {
199-
int x = peaks[i][0], y = peaks[i][1];
200-
arr[i] = new int[] {x - y, x + y};
201-
}
202-
Arrays.sort(arr, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
203-
int ans = 0;
204-
int cur = Integer.MIN_VALUE;
205-
for (int i = 0; i < n; ++i) {
206-
int l = arr[i][0], r = arr[i][1];
207-
if (r <= cur) {
208-
continue;
209-
}
210-
cur = r;
211-
if (!(i < n - 1 && arr[i][0] == arr[i + 1][0] && arr[i][1] == arr[i + 1][1])) {
212-
++ans;
213-
}
214-
}
215-
return ans;
216-
}
217-
}
218-
```
219-
220-
<!-- tabs:end -->
221-
222-
<!-- solution:end -->
223-
224183
<!-- problem:end -->

solution/2300-2399/2345.Finding the Number of Visible Mountains/README_EN.md

+12-45
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,15 @@ Both mountains are not visible since their peaks lie within each other.
6161

6262
<!-- solution:start -->
6363

64-
### Solution 1
64+
### Solution 1: Interval Sorting + Traversal
65+
66+
We first convert each mountain $(x, y)$ into a horizontal interval $(x - y, x + y)$, then sort the intervals by left endpoint in ascending order and right endpoint in descending order.
67+
68+
Next, we initialize the right endpoint of the current interval as $-\infty$. We traverse each mountain. If the right endpoint of the current mountain is less than or equal to the right endpoint of the current interval, we skip this mountain. Otherwise, we update the right endpoint of the current interval to the right endpoint of the current mountain. If the interval of the current mountain appears only once, we increment the answer.
69+
70+
Finally, we return the answer.
71+
72+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the number of mountains.
6573

6674
<!-- tabs:start -->
6775

@@ -90,22 +98,20 @@ class Solution {
9098
public int visibleMountains(int[][] peaks) {
9199
int n = peaks.length;
92100
int[][] arr = new int[n][2];
93-
Map<String, Integer> cnt = new HashMap<>();
94101
for (int i = 0; i < n; ++i) {
95102
int x = peaks[i][0], y = peaks[i][1];
96103
arr[i] = new int[] {x - y, x + y};
97-
cnt.merge((x - y) + "" + (x + y), 1, Integer::sum);
98104
}
99105
Arrays.sort(arr, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
100106
int ans = 0;
101107
int cur = Integer.MIN_VALUE;
102-
for (int[] e : arr) {
103-
int l = e[0], r = e[1];
108+
for (int i = 0; i < n; ++i) {
109+
int l = arr[i][0], r = arr[i][1];
104110
if (r <= cur) {
105111
continue;
106112
}
107113
cur = r;
108-
if (cnt.get(l + "" + r) == 1) {
114+
if (!(i < n - 1 && arr[i][0] == arr[i + 1][0] && arr[i][1] == arr[i + 1][1])) {
109115
++ans;
110116
}
111117
}
@@ -172,43 +178,4 @@ func visibleMountains(peaks [][]int) (ans int) {
172178

173179
<!-- solution:end -->
174180

175-
<!-- solution:start -->
176-
177-
### Solution 2
178-
179-
<!-- tabs:start -->
180-
181-
#### Java
182-
183-
```java
184-
class Solution {
185-
public int visibleMountains(int[][] peaks) {
186-
int n = peaks.length;
187-
int[][] arr = new int[n][2];
188-
for (int i = 0; i < n; ++i) {
189-
int x = peaks[i][0], y = peaks[i][1];
190-
arr[i] = new int[] {x - y, x + y};
191-
}
192-
Arrays.sort(arr, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
193-
int ans = 0;
194-
int cur = Integer.MIN_VALUE;
195-
for (int i = 0; i < n; ++i) {
196-
int l = arr[i][0], r = arr[i][1];
197-
if (r <= cur) {
198-
continue;
199-
}
200-
cur = r;
201-
if (!(i < n - 1 && arr[i][0] == arr[i + 1][0] && arr[i][1] == arr[i + 1][1])) {
202-
++ans;
203-
}
204-
}
205-
return ans;
206-
}
207-
}
208-
```
209-
210-
<!-- tabs:end -->
211-
212-
<!-- solution:end -->
213-
214181
<!-- problem:end -->

solution/2300-2399/2345.Finding the Number of Visible Mountains/Solution.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,23 @@ class Solution {
22
public int visibleMountains(int[][] peaks) {
33
int n = peaks.length;
44
int[][] arr = new int[n][2];
5-
Map<String, Integer> cnt = new HashMap<>();
65
for (int i = 0; i < n; ++i) {
76
int x = peaks[i][0], y = peaks[i][1];
87
arr[i] = new int[] {x - y, x + y};
9-
cnt.merge((x - y) + "" + (x + y), 1, Integer::sum);
108
}
119
Arrays.sort(arr, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
1210
int ans = 0;
1311
int cur = Integer.MIN_VALUE;
14-
for (int[] e : arr) {
15-
int l = e[0], r = e[1];
12+
for (int i = 0; i < n; ++i) {
13+
int l = arr[i][0], r = arr[i][1];
1614
if (r <= cur) {
1715
continue;
1816
}
1917
cur = r;
20-
if (cnt.get(l + "" + r) == 1) {
18+
if (!(i < n - 1 && arr[i][0] == arr[i + 1][0] && arr[i][1] == arr[i + 1][1])) {
2119
++ans;
2220
}
2321
}
2422
return ans;
2523
}
26-
}
24+
}

solution/2300-2399/2345.Finding the Number of Visible Mountains/Solution2.java

-24
This file was deleted.

solution/2300-2399/2347.Best Poker Hand/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ tags:
7979

8080
### 方法一:计数
8181

82-
我们可以先遍历数组 $suits$,判断相邻两个元素是否均相等,如果是,则返回 `"Flush"`
82+
我们可以先遍历数组 $\textit{suits}$,判断相邻两个元素是否均相等,如果是,则返回 `"Flush"`
8383

84-
接下来,我们用哈希表或数组 $cnt$ 统计每张牌的数量:
84+
接下来,我们用哈希表或数组 $\textit{cnt}$ 统计每张牌的数量:
8585

8686
- 如果有任意一张牌的数量等于 $3$,返回 `"Three of a Kind"`
8787
- 否则,如果有任意一张牌的数量等于 $2$,返回 `"Pair"`
8888
- 否则,返回 `"High Card"`
8989

90-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $ranks$ 的长度。
90+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{ranks}$ 的长度。
9191

9292
<!-- tabs:start -->
9393

solution/2300-2399/2347.Best Poker Hand/README_EN.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,17 @@ Note that we cannot make a &quot;Flush&quot; or a &quot;Three of a Kind&quot;.
7878

7979
<!-- solution:start -->
8080

81-
### Solution 1
81+
### Solution 1: Counting
82+
83+
We first traverse the array $\textit{suits}$ to check if adjacent elements are equal. If they are, we return `"Flush"`.
84+
85+
Next, we use a hash table or array $\textit{cnt}$ to count the quantity of each card:
86+
87+
- If any card appears $3$ times, return `"Three of a Kind"`;
88+
- Otherwise, if any card appears $2$ times, return `"Pair"`;
89+
- Otherwise, return `"High Card"`.
90+
91+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{ranks}$.
8292

8393
<!-- tabs:start -->
8494

0 commit comments

Comments
 (0)