Skip to content

Commit

Permalink
feat: update solutions to lc problems: No.2340~2347 (#3838)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme authored Dec 4, 2024
1 parent 3b4a836 commit e761584
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 164 deletions.
28 changes: 0 additions & 28 deletions .github/workflows/pr-add-label.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ tags:

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

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

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

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

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,17 @@ It can be shown that 6 swaps is the minimum swaps required to make a valid array

<!-- solution:start -->

### Solution 1
### Solution 1: Maintain Index of Extremes + Case Analysis

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$.

Next, we need to consider the number of swaps.

- If $i = j$, it means the array $\textit{nums}$ is already a valid array, and no swaps are needed. Return $0$.
- 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}$.
- 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$.

The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ nums[0] 和 nums[1] 形成一个数对,并从 nums 中移除,nums = [2] 。

### 方法一:计数

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

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

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

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

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,17 @@ No more pairs can be formed. A total of 1 pair has been formed, and there are 0

<!-- solution:start -->

### Solution 1
### Solution 1: Counting

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}$.

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$.

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$.

The answer is $[s, n - s \times 2]$.

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.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ tags:

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

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Simulation

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.

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]$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,20 @@ class Solution {
public int visibleMountains(int[][] peaks) {
int n = peaks.length;
int[][] arr = new int[n][2];
Map<String, Integer> cnt = new HashMap<>();
for (int i = 0; i < n; ++i) {
int x = peaks[i][0], y = peaks[i][1];
arr[i] = new int[] {x - y, x + y};
cnt.merge((x - y) + "" + (x + y), 1, Integer::sum);
}
Arrays.sort(arr, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
int ans = 0;
int cur = Integer.MIN_VALUE;
for (int[] e : arr) {
int l = e[0], r = e[1];
for (int i = 0; i < n; ++i) {
int l = arr[i][0], r = arr[i][1];
if (r <= cur) {
continue;
}
cur = r;
if (cnt.get(l + "" + r) == 1) {
if (!(i < n - 1 && arr[i][0] == arr[i + 1][0] && arr[i][1] == arr[i + 1][1])) {
++ans;
}
}
Expand Down Expand Up @@ -182,43 +180,4 @@ func visibleMountains(peaks [][]int) (ans int) {

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Java

```java
class Solution {
public int visibleMountains(int[][] peaks) {
int n = peaks.length;
int[][] arr = new int[n][2];
for (int i = 0; i < n; ++i) {
int x = peaks[i][0], y = peaks[i][1];
arr[i] = new int[] {x - y, x + y};
}
Arrays.sort(arr, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
int ans = 0;
int cur = Integer.MIN_VALUE;
for (int i = 0; i < n; ++i) {
int l = arr[i][0], r = arr[i][1];
if (r <= cur) {
continue;
}
cur = r;
if (!(i < n - 1 && arr[i][0] == arr[i + 1][0] && arr[i][1] == arr[i + 1][1])) {
++ans;
}
}
return ans;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,15 @@ Both mountains are not visible since their peaks lie within each other.

<!-- solution:start -->

### Solution 1
### Solution 1: Interval Sorting + Traversal

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.

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.

Finally, we return the answer.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the number of mountains.

<!-- tabs:start -->

Expand Down Expand Up @@ -90,22 +98,20 @@ class Solution {
public int visibleMountains(int[][] peaks) {
int n = peaks.length;
int[][] arr = new int[n][2];
Map<String, Integer> cnt = new HashMap<>();
for (int i = 0; i < n; ++i) {
int x = peaks[i][0], y = peaks[i][1];
arr[i] = new int[] {x - y, x + y};
cnt.merge((x - y) + "" + (x + y), 1, Integer::sum);
}
Arrays.sort(arr, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
int ans = 0;
int cur = Integer.MIN_VALUE;
for (int[] e : arr) {
int l = e[0], r = e[1];
for (int i = 0; i < n; ++i) {
int l = arr[i][0], r = arr[i][1];
if (r <= cur) {
continue;
}
cur = r;
if (cnt.get(l + "" + r) == 1) {
if (!(i < n - 1 && arr[i][0] == arr[i + 1][0] && arr[i][1] == arr[i + 1][1])) {
++ans;
}
}
Expand Down Expand Up @@ -172,43 +178,4 @@ func visibleMountains(peaks [][]int) (ans int) {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### Java

```java
class Solution {
public int visibleMountains(int[][] peaks) {
int n = peaks.length;
int[][] arr = new int[n][2];
for (int i = 0; i < n; ++i) {
int x = peaks[i][0], y = peaks[i][1];
arr[i] = new int[] {x - y, x + y};
}
Arrays.sort(arr, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
int ans = 0;
int cur = Integer.MIN_VALUE;
for (int i = 0; i < n; ++i) {
int l = arr[i][0], r = arr[i][1];
if (r <= cur) {
continue;
}
cur = r;
if (!(i < n - 1 && arr[i][0] == arr[i + 1][0] && arr[i][1] == arr[i + 1][1])) {
++ans;
}
}
return ans;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@ class Solution {
public int visibleMountains(int[][] peaks) {
int n = peaks.length;
int[][] arr = new int[n][2];
Map<String, Integer> cnt = new HashMap<>();
for (int i = 0; i < n; ++i) {
int x = peaks[i][0], y = peaks[i][1];
arr[i] = new int[] {x - y, x + y};
cnt.merge((x - y) + "" + (x + y), 1, Integer::sum);
}
Arrays.sort(arr, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
int ans = 0;
int cur = Integer.MIN_VALUE;
for (int[] e : arr) {
int l = e[0], r = e[1];
for (int i = 0; i < n; ++i) {
int l = arr[i][0], r = arr[i][1];
if (r <= cur) {
continue;
}
cur = r;
if (cnt.get(l + "" + r) == 1) {
if (!(i < n - 1 && arr[i][0] == arr[i + 1][0] && arr[i][1] == arr[i + 1][1])) {
++ans;
}
}
return ans;
}
}
}

This file was deleted.

6 changes: 3 additions & 3 deletions solution/2300-2399/2347.Best Poker Hand/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ tags:

### 方法一:计数

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

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

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

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

<!-- tabs:start -->

Expand Down
12 changes: 11 additions & 1 deletion solution/2300-2399/2347.Best Poker Hand/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,17 @@ Note that we cannot make a &quot;Flush&quot; or a &quot;Three of a Kind&quot;.

<!-- solution:start -->

### Solution 1
### Solution 1: Counting

We first traverse the array $\textit{suits}$ to check if adjacent elements are equal. If they are, we return `"Flush"`.

Next, we use a hash table or array $\textit{cnt}$ to count the quantity of each card:

- If any card appears $3$ times, return `"Three of a Kind"`;
- Otherwise, if any card appears $2$ times, return `"Pair"`;
- Otherwise, return `"High Card"`.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{ranks}$.

<!-- tabs:start -->

Expand Down

0 comments on commit e761584

Please sign in to comment.