Skip to content

Commit 207245b

Browse files
authored
feat: add solutions to lc problems: No.2481+ (doocs#2372)
1 parent 67e2be0 commit 207245b

File tree

12 files changed

+112
-15
lines changed

12 files changed

+112
-15
lines changed

solution/2400-2499/2481.Minimum Cuts to Divide a Circle/README_EN.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,22 @@ Also note that the first cut will not divide the circle into distinct parts.
4747

4848
## Solutions
4949

50-
### Solution 1
50+
### Solution 1: Case Discussion
51+
52+
- When $n=1$, no cutting is needed, so the number of cuts is $0$;
53+
- When $n$ is odd, there is no collinear situation, and at least $n$ cuts are needed;
54+
- When $n$ is even, they can be collinear in pairs, and at least $\frac{n}{2}$ cuts are needed.
55+
56+
In summary, we can get:
57+
58+
$$
59+
\text{ans} = \begin{cases}
60+
n, & n \gt 1 \text{ and } n \text{ is odd} \\
61+
\frac{n}{2}, & n \text{ is even} \\
62+
\end{cases}
63+
$$
64+
65+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
5166

5267
<!-- tabs:start -->
5368

solution/2400-2499/2482.Difference Between Ones and Zeros in Row and Column/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@
6565

6666
## Solutions
6767

68-
### Solution 1
68+
### Solution 1: Simulation
69+
70+
We can solve this problem by simulating the process as described in the problem statement.
71+
72+
The time complexity is $O(m \times n)$, and if we ignore the space used by the answer, the space complexity is $O(m + n)$. Here, $m$ and $n$ are the number of rows and columns in the matrix, respectively.
6973

7074
<!-- tabs:start -->
7175

solution/2400-2499/2483.Minimum Penalty for a Shop/README_EN.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ Closing the shop at 2<sup>nd</sup> or 4<sup>th</sup> hour gives a minimum penalt
6464

6565
## Solutions
6666

67-
### Solution 1
67+
### Solution 1: Prefix Sum + Enumeration
68+
69+
First, we calculate how many customers arrive in the first $i$ hours and record it in the prefix sum array $s$.
70+
71+
Then we enumerate the closing time $j$ of the shop, calculate the cost, and take the earliest closing time with the smallest cost.
72+
73+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string $customers$.
6874

6975
<!-- tabs:start -->
7076

solution/2400-2499/2484.Count Palindromic Subsequences/README_EN.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ Two of them (both equal to &quot;10301&quot;) are palindromic.
5252

5353
## Solutions
5454

55-
### Solution 1
55+
### Solution 1: Enumeration + Counting
56+
57+
The time complexity is $O(100 \times n)$, and the space complexity is $O(100 \times n)$. Where $n$ is the length of the string $s$.
5658

5759
<!-- tabs:start -->
5860

solution/2400-2499/2488.Count Subarrays With Median K/README_EN.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,21 @@
5252

5353
## Solutions
5454

55-
### Solution 1
55+
### Solution 1: Traversal + Counting
56+
57+
First, we find the position $i$ of the median $k$ in the array, and then start traversing from $i$ to both sides, counting the number of subarrays with a median of $k$.
58+
59+
Define an answer variable $ans$, which represents the number of subarrays with a median of $k$. Initially, $ans = 1$, which means that there is currently a subarray of length $1$ with a median of $k$. In addition, define a counter $cnt$, used to count the number of differences between the "number of elements larger than $k$" and the "number of elements smaller than $k$" in the currently traversed array.
60+
61+
Next, start traversing to the right from $i + 1$. We maintain a variable $x$, which represents the difference between the "number of elements larger than $k$" and the "number of elements smaller than $k$" in the current right subarray. If $x \in [0, 1]$, then the median of the current right subarray is $k$, and the answer variable $ans$ is incremented by $1$. Then, we add the value of $x$ to the counter $cnt$.
62+
63+
Similarly, start traversing to the left from $i - 1$, also maintaining a variable $x$, which represents the difference between the "number of elements larger than $k$" and the "number of elements smaller than $k$" in the current left subarray. If $x \in [0, 1]$, then the median of the current left subarray is $k$, and the answer variable $ans$ is incremented by $1$. If $-x$ or $-x + 1$ is also in the counter, it means that there is currently a subarray that spans both sides of $i$, with a median of $k$, and the answer variable $ans$ increases the corresponding value in the counter, that is, $ans += cnt[-x] + cnt[-x + 1]$.
64+
65+
Finally, return the answer variable $ans$.
66+
67+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array.
68+
69+
> In coding, we can directly open an array of length $2 \times n + 1$, used to count the difference between the "number of elements larger than $k$" and the "number of elements smaller than $k$" in the current array. Each time we add the difference by $n$, we can convert the range of the difference from $[-n, n]$ to $[0, 2n]$.
5670
5771
<!-- tabs:start -->
5872

solution/2400-2499/2489.Number of Substrings With Fixed Ratio/README_EN.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,25 @@ It can be shown that there are no more ratio substrings.
5656

5757
## Solutions
5858

59-
### Solution 1
59+
### Solution 1: Prefix Sum + Counting
60+
61+
We use $one[i]$ to represent the number of $1$s in the substring $s[0,..i]$, and $zero[i]$ to represent the number of $0$s in the substring $s[0,..i]$. A substring meets the condition if
62+
63+
$$
64+
\frac{zero[j] - zero[i]}{one[j] - one[i]} = \frac{num1}{num2}
65+
$$
66+
67+
where $i < j$. We can transform the above equation into
68+
69+
$$
70+
one[j] \times num1 - zero[j] \times num2 = one[i] \times num1 - zero[i] \times num2
71+
$$
72+
73+
When we iterate to index $j$, we only need to count how many indices $i$ satisfy the above equation. Therefore, we can use a hash table to record the number of occurrences of $one[i] \times num1 - zero[i] \times num2$, and when we iterate to index $j$, we only need to count the number of occurrences of $one[j] \times num1 - zero[j] \times num2$.
74+
75+
The hash table initially only has one key-value pair $(0, 1)$.
76+
77+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string $s$.
6078

6179
<!-- tabs:start -->
6280

solution/2400-2499/2490.Circular Sentence/README_EN.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ The sentence is <strong>not</strong> circular.</pre>
6868

6969
## Solutions
7070

71-
### Solution 1
71+
### Solution 1: Simulation
72+
73+
We split the string into words by spaces, then check whether the last character of each word is equal to the first character of the next word. If they are not equal, return `false`. Otherwise, return `true` after traversing all the words.
74+
75+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string.
7276

7377
<!-- tabs:start -->
7478

@@ -181,7 +185,11 @@ var isCircularSentence = function (sentence) {
181185

182186
<!-- tabs:end -->
183187

184-
### Solution 2
188+
### Solution 2: Simulation (Space Optimization)
189+
190+
We can first check whether the first and last characters of the string are equal. If they are not equal, return `false`. Otherwise, traverse the string. If the current character is a space, check whether the previous character and the next character are equal. If they are not equal, return `false`. Otherwise, return `true` after traversing all the characters.
191+
192+
The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(1)$.
185193

186194
<!-- tabs:start -->
187195

solution/2400-2499/2491.Divide Players Into Teams of Equal Skill/README_EN.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,15 @@ There is no way to divide the players into teams such that the total skill of ea
5353

5454
## Solutions
5555

56-
### Solution 1
56+
### Solution 1: Sorting
57+
58+
To make all 2-person teams have equal skill points, the minimum value must match the maximum value. Therefore, we sort the `skill` array, and then use two pointers $i$ and $j$ to point to the beginning and end of the array respectively, match them in pairs, and judge whether their sum is the same number.
59+
60+
If not, it means that the skill points cannot be equal, and we directly return $-1$. Otherwise, we add the chemical reaction to the answer.
61+
62+
At the end of the traversal, we return the answer.
63+
64+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Where $n$ is the length of the `skill` array.
5765

5866
<!-- tabs:start -->
5967

@@ -175,7 +183,9 @@ var dividePlayers = function (skill) {
175183

176184
<!-- tabs:end -->
177185

178-
### Solution 2
186+
### Solution 2: Counting
187+
188+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the `skill` array.
179189

180190
<!-- tabs:start -->
181191

solution/2400-2499/2492.Minimum Score of a Path Between Two Cities/README_EN.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ It can be shown that no other path has less score.
5454

5555
## Solutions
5656

57-
### Solution 1
57+
### Solution 1: DFS
58+
59+
According to the problem description, each edge can be passed multiple times, and it is guaranteed that node $1$ and node $n$ are in the same connected component. Therefore, the problem is actually looking for the smallest edge in the connected component where node $1$ is located. We can use DFS, start searching from node $1$, and find the smallest edge.
60+
61+
The time complexity is $O(n + m)$, where $n$ and $m$ are the number of nodes and edges, respectively.
5862

5963
<!-- tabs:start -->
6064

@@ -246,7 +250,11 @@ var minScore = function (n, roads) {
246250

247251
<!-- tabs:end -->
248252

249-
### Solution 2
253+
### Solution 2: BFS
254+
255+
We can also use BFS to solve this problem.
256+
257+
The time complexity is $O(n + m)$, where $n$ and $m$ are the number of nodes and edges, respectively.
250258

251259
<!-- tabs:start -->
252260

solution/2400-2499/2493.Divide Nodes Into the Maximum Number of Groups/README_EN.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ It can be shown that no grouping is possible.
5757

5858
## Solutions
5959

60-
### Solution 1
60+
### Solution 1: DFS + BFS
61+
62+
Notice that the graph may not be connected, so we can find each connected component through DFS.
63+
64+
Then for each connected component, enumerate each node in the component as the starting point, and use BFS to layer the graph. After the layering is finished, check whether it is valid. If it is valid, update the maximum value of the connected component. If there is no valid layering in a connected component, it means that it is impossible to group under the given conditions, and directly return $-1$. Otherwise, add the maximum value of the connected component to the answer.
65+
66+
The time complexity is $O(n \times (n + m))$, and the space complexity is $O(n + m)$. Where $n$ and $m$ are the number of nodes and edges, respectively.
6167

6268
<!-- tabs:start -->
6369

solution/2400-2499/2495.Number of Subarrays Having Even Product/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454
因此,我们可以遍历数组,记录最近一个偶数的下标 `last`,则以当前元素结尾的子数组中,乘积为偶数的子数组个数为 `last + 1`,累加到结果中即可。
5555

56-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
56+
时间复杂度 $O(n)$,其中 $n$ 为数组 `nums` 的长度。空间复杂度 $O(1)$
5757

5858
<!-- tabs:start -->
5959

solution/2400-2499/2495.Number of Subarrays Having Even Product/README_EN.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@
4141

4242
## Solutions
4343

44-
### Solution 1
44+
### Solution 1: Single Pass
45+
46+
We know that the product of a subarray is even if and only if there is at least one even number in the subarray.
47+
48+
Therefore, we can traverse the array, record the index `last` of the most recent even number, then the number of subarrays ending with the current element and having an even product is `last + 1`. We can add this to the result.
49+
50+
The time complexity is $O(n)$, where $n$ is the length of the array `nums`. The space complexity is $O(1)$.
4551

4652
<!-- tabs:start -->
4753

0 commit comments

Comments
 (0)