You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2300-2399/2340.Minimum Adjacent Swaps to Make a Valid Array/README_EN.md
+11-1
Original file line number
Diff line number
Diff line change
@@ -68,7 +68,17 @@ It can be shown that 6 swaps is the minimum swaps required to make a valid array
68
68
69
69
<!-- solution:start -->
70
70
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)$.
Copy file name to clipboardexpand all lines: solution/2300-2399/2341.Maximum Number of Pairs in Array/README_EN.md
+11-1
Original file line number
Diff line number
Diff line change
@@ -75,7 +75,17 @@ No more pairs can be formed. A total of 1 pair has been formed, and there are 0
75
75
76
76
<!-- solution:start -->
77
77
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.
Copy file name to clipboardexpand all lines: solution/2300-2399/2343.Query Kth Smallest Trimmed Number/README_EN.md
+5-1
Original file line number
Diff line number
Diff line change
@@ -91,7 +91,11 @@ tags:
91
91
92
92
<!-- solution:start -->
93
93
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]$.
Copy file name to clipboardexpand all lines: solution/2300-2399/2345.Finding the Number of Visible Mountains/README_EN.md
+12-45
Original file line number
Diff line number
Diff line change
@@ -61,7 +61,15 @@ Both mountains are not visible since their peaks lie within each other.
61
61
62
62
<!-- solution:start -->
63
63
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.
0 commit comments