Skip to content

feat: add solutions to lc problems: No.2481+ #2372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,22 @@ Also note that the first cut will not divide the circle into distinct parts.

## Solutions

### Solution 1
### Solution 1: Case Discussion

- When $n=1$, no cutting is needed, so the number of cuts is $0$;
- When $n$ is odd, there is no collinear situation, and at least $n$ cuts are needed;
- When $n$ is even, they can be collinear in pairs, and at least $\frac{n}{2}$ cuts are needed.

In summary, we can get:

$$
\text{ans} = \begin{cases}
n, & n \gt 1 \text{ and } n \text{ is odd} \\
\frac{n}{2}, & n \text{ is even} \\
\end{cases}
$$

The time complexity is $O(1)$, and the space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@

## Solutions

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

We can solve this problem by simulating the process as described in the problem statement.

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.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ Closing the shop at 2<sup>nd</sup> or 4<sup>th</sup> hour gives a minimum penalt

## Solutions

### Solution 1
### Solution 1: Prefix Sum + Enumeration

First, we calculate how many customers arrive in the first $i$ hours and record it in the prefix sum array $s$.

Then we enumerate the closing time $j$ of the shop, calculate the cost, and take the earliest closing time with the smallest cost.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string $customers$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ Two of them (both equal to &quot;10301&quot;) are palindromic.

## Solutions

### Solution 1
### Solution 1: Enumeration + Counting

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,21 @@

## Solutions

### Solution 1
### Solution 1: Traversal + Counting

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

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.

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

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

Finally, return the answer variable $ans$.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array.

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,25 @@ It can be shown that there are no more ratio substrings.

## Solutions

### Solution 1
### Solution 1: Prefix Sum + Counting

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

$$
\frac{zero[j] - zero[i]}{one[j] - one[i]} = \frac{num1}{num2}
$$

where $i < j$. We can transform the above equation into

$$
one[j] \times num1 - zero[j] \times num2 = one[i] \times num1 - zero[i] \times num2
$$

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

The hash table initially only has one key-value pair $(0, 1)$.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string $s$.

<!-- tabs:start -->

Expand Down
12 changes: 10 additions & 2 deletions solution/2400-2499/2490.Circular Sentence/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ The sentence is <strong>not</strong> circular.</pre>

## Solutions

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

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.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string.

<!-- tabs:start -->

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

<!-- tabs:end -->

### Solution 2
### Solution 2: Simulation (Space Optimization)

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.

The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ There is no way to divide the players into teams such that the total skill of ea

## Solutions

### Solution 1
### Solution 1: Sorting

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.

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.

At the end of the traversal, we return the answer.

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.

<!-- tabs:start -->

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

<!-- tabs:end -->

### Solution 2
### Solution 2: Counting

The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the `skill` array.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ It can be shown that no other path has less score.

## Solutions

### Solution 1
### Solution 1: DFS

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.

The time complexity is $O(n + m)$, where $n$ and $m$ are the number of nodes and edges, respectively.

<!-- tabs:start -->

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

<!-- tabs:end -->

### Solution 2
### Solution 2: BFS

We can also use BFS to solve this problem.

The time complexity is $O(n + m)$, where $n$ and $m$ are the number of nodes and edges, respectively.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ It can be shown that no grouping is possible.

## Solutions

### Solution 1
### Solution 1: DFS + BFS

Notice that the graph may not be connected, so we can find each connected component through DFS.

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.

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.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

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

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@

## Solutions

### Solution 1
### Solution 1: Single Pass

We know that the product of a subarray is even if and only if there is at least one even number in the subarray.

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.

The time complexity is $O(n)$, where $n$ is the length of the array `nums`. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down