diff --git a/.github/workflows/pr-add-label.yml b/.github/workflows/pr-add-label.yml deleted file mode 100644 index aeadb540a0666..0000000000000 --- a/.github/workflows/pr-add-label.yml +++ /dev/null @@ -1,28 +0,0 @@ -name: pr-add-label - -on: - pull_request_target: - types: [opened, edited, reopened, synchronize] - -concurrency: - group: ${{github.workflow}} - ${{github.event_name}} - cancel-in-progress: true - -jobs: - add-label: - permissions: - contents: read - pull-requests: write - runs-on: ubuntu-latest - steps: - - name: Check PR number - id: pr_number - run: echo "PR_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV - - - name: Run add-label Action - uses: thinkasany/pr-label-action@master - with: - github_token: ${{ secrets.DOOCS_BOT_ACTION_TOKEN }} - pr_number: ${{ env.PR_NUMBER }} - organize_name: "doocs" - team_name: "leetcode-algorithm" diff --git a/solution/2300-2399/2340.Minimum Adjacent Swaps to Make a Valid Array/README.md b/solution/2300-2399/2340.Minimum Adjacent Swaps to Make a Valid Array/README.md index dcf7e9f0d5e7d..1977d70bfe3f3 100644 --- a/solution/2300-2399/2340.Minimum Adjacent Swaps to Make a Valid Array/README.md +++ b/solution/2300-2399/2340.Minimum Adjacent Swaps to Make a Valid Array/README.md @@ -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)$。 diff --git a/solution/2300-2399/2340.Minimum Adjacent Swaps to Make a Valid Array/README_EN.md b/solution/2300-2399/2340.Minimum Adjacent Swaps to Make a Valid Array/README_EN.md index 2e358b52eee24..a998054c7b29a 100644 --- a/solution/2300-2399/2340.Minimum Adjacent Swaps to Make a Valid Array/README_EN.md +++ b/solution/2300-2399/2340.Minimum Adjacent Swaps to Make a Valid Array/README_EN.md @@ -68,7 +68,17 @@ It can be shown that 6 swaps is the minimum swaps required to make a valid array -### 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)$. diff --git a/solution/2300-2399/2341.Maximum Number of Pairs in Array/README.md b/solution/2300-2399/2341.Maximum Number of Pairs in Array/README.md index 2cd351206bf00..a6a2f65f64473 100644 --- a/solution/2300-2399/2341.Maximum Number of Pairs in Array/README.md +++ b/solution/2300-2399/2341.Maximum Number of Pairs in Array/README.md @@ -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$。 diff --git a/solution/2300-2399/2341.Maximum Number of Pairs in Array/README_EN.md b/solution/2300-2399/2341.Maximum Number of Pairs in Array/README_EN.md index 73ad1a25d51af..940fc07c575a5 100644 --- a/solution/2300-2399/2341.Maximum Number of Pairs in Array/README_EN.md +++ b/solution/2300-2399/2341.Maximum Number of Pairs in Array/README_EN.md @@ -75,7 +75,17 @@ No more pairs can be formed. A total of 1 pair has been formed, and there are 0 -### 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. diff --git a/solution/2300-2399/2343.Query Kth Smallest Trimmed Number/README.md b/solution/2300-2399/2343.Query Kth Smallest Trimmed Number/README.md index 22c53442ab950..f75ac75ef1e0d 100644 --- a/solution/2300-2399/2343.Query Kth Smallest Trimmed Number/README.md +++ b/solution/2300-2399/2343.Query Kth Smallest Trimmed Number/README.md @@ -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]$ 字符串的长度。 diff --git a/solution/2300-2399/2343.Query Kth Smallest Trimmed Number/README_EN.md b/solution/2300-2399/2343.Query Kth Smallest Trimmed Number/README_EN.md index 303323a701a66..c01cd8b70191d 100644 --- a/solution/2300-2399/2343.Query Kth Smallest Trimmed Number/README_EN.md +++ b/solution/2300-2399/2343.Query Kth Smallest Trimmed Number/README_EN.md @@ -91,7 +91,11 @@ tags: -### 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]$. diff --git a/solution/2300-2399/2345.Finding the Number of Visible Mountains/README.md b/solution/2300-2399/2345.Finding the Number of Visible Mountains/README.md index d684e9f107ef9..8b1d7c48030f3 100644 --- a/solution/2300-2399/2345.Finding the Number of Visible Mountains/README.md +++ b/solution/2300-2399/2345.Finding the Number of Visible Mountains/README.md @@ -100,22 +100,20 @@ class Solution { public int visibleMountains(int[][] peaks) { int n = peaks.length; int[][] arr = new int[n][2]; - Map 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; } } @@ -182,43 +180,4 @@ func visibleMountains(peaks [][]int) (ans int) { - - -### 方法二 - - - -#### 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; - } -} -``` - - - - - diff --git a/solution/2300-2399/2345.Finding the Number of Visible Mountains/README_EN.md b/solution/2300-2399/2345.Finding the Number of Visible Mountains/README_EN.md index f888cd86deeb0..79aad7c0fb0fb 100644 --- a/solution/2300-2399/2345.Finding the Number of Visible Mountains/README_EN.md +++ b/solution/2300-2399/2345.Finding the Number of Visible Mountains/README_EN.md @@ -61,7 +61,15 @@ Both mountains are not visible since their peaks lie within each other. -### 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. @@ -90,22 +98,20 @@ class Solution { public int visibleMountains(int[][] peaks) { int n = peaks.length; int[][] arr = new int[n][2]; - Map 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; } } @@ -172,43 +178,4 @@ func visibleMountains(peaks [][]int) (ans int) { - - -### Solution 2 - - - -#### 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; - } -} -``` - - - - - diff --git a/solution/2300-2399/2345.Finding the Number of Visible Mountains/Solution.java b/solution/2300-2399/2345.Finding the Number of Visible Mountains/Solution.java index f2d4cec650b88..414bc70a44539 100644 --- a/solution/2300-2399/2345.Finding the Number of Visible Mountains/Solution.java +++ b/solution/2300-2399/2345.Finding the Number of Visible Mountains/Solution.java @@ -2,25 +2,23 @@ class Solution { public int visibleMountains(int[][] peaks) { int n = peaks.length; int[][] arr = new int[n][2]; - Map 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; } -} \ No newline at end of file +} diff --git a/solution/2300-2399/2345.Finding the Number of Visible Mountains/Solution2.java b/solution/2300-2399/2345.Finding the Number of Visible Mountains/Solution2.java deleted file mode 100644 index 52771483480e4..0000000000000 --- a/solution/2300-2399/2345.Finding the Number of Visible Mountains/Solution2.java +++ /dev/null @@ -1,24 +0,0 @@ -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; - } -} \ No newline at end of file diff --git a/solution/2300-2399/2347.Best Poker Hand/README.md b/solution/2300-2399/2347.Best Poker Hand/README.md index 3adcdd0d65d7c..220076ed64c73 100644 --- a/solution/2300-2399/2347.Best Poker Hand/README.md +++ b/solution/2300-2399/2347.Best Poker Hand/README.md @@ -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}$ 的长度。 diff --git a/solution/2300-2399/2347.Best Poker Hand/README_EN.md b/solution/2300-2399/2347.Best Poker Hand/README_EN.md index 5c35ef13e94a8..b715851f68bb2 100644 --- a/solution/2300-2399/2347.Best Poker Hand/README_EN.md +++ b/solution/2300-2399/2347.Best Poker Hand/README_EN.md @@ -78,7 +78,17 @@ Note that we cannot make a "Flush" or a "Three of a Kind". -### 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}$.