Skip to content

feat: update solutions to lc problems: No.2251,2259 #3791

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
Nov 20, 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 @@ -73,7 +73,7 @@ tags:

我们将花按照开始时间和结束时间分别排序,然后对于每个人,我们可以使用二分查找来找到他们到达时在花期内花的数目。就是说,找出在每个人到达时,已经开花的花的数目,减去在每个人到达时,已经凋谢的花的数目,即可得到答案。

时间复杂度 $O((m + n) \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是数组 $flowers$ 和 $people$ 的长度。
时间复杂度 $O((m + n) \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是数组 $\textit{flowers}$ 和 $\textit{people}$ 的长度。

<!-- tabs:start -->

Expand Down Expand Up @@ -269,7 +269,7 @@ impl Solution {

我们可以利用差分来维护每个时间点的花的数目。接下来,我们将 $people$ 按照到达时间从小到大排序,在每个人到达时,我们对差分数组进行前缀和运算,就可以得到答案。

时间复杂度 $O(m \times \log m + n \times \log n)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别是数组 $flowers$ 和 $people$ 的长度。
时间复杂度 $O(m \times \log m + n \times \log n)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别是数组 $\textit{flowers}$ 和 $\textit{people}$ 的长度。

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ For each person, we return the number of flowers in full bloom during their arri

<!-- solution:start -->

### Solution 1
### Solution 1: Sorting + Binary Search

We sort the flowers by their start and end times. Then, for each person, we can use binary search to find the number of flowers in bloom when they arrive. This means finding the number of flowers that have started blooming by the time each person arrives, minus the number of flowers that have wilted by that time, to get the answer.

The time complexity is $O((m + n) \times \log n)$, and the space complexity is $O(n)$. Here, $n$ and $m$ are the lengths of the arrays $\textit{flowers}$ and $\textit{people}$, respectively.

<!-- tabs:start -->

Expand Down Expand Up @@ -255,7 +259,11 @@ impl Solution {

<!-- solution:start -->

### Solution 2
### Solution 2: Difference Array + Sorting + Offline Query

We can use a difference array to maintain the number of flowers at each time point. Next, we sort $people$ by their arrival times in ascending order. When each person arrives, we perform a prefix sum operation on the difference array to get the answer.

The time complexity is $O(m \times \log m + n \times \log n)$, and the space complexity is $O(n + m)$. Here, $n$ and $m$ are the lengths of the arrays $\textit{flowers}$ and $\textit{people}$, respectively.

<!-- tabs:start -->

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

### 方法一:暴力枚举

我们可以枚举字符串 $number$ 的所有位置 $i$,如果 $number[i] = digit$,那么我们取 $number$ 的前缀 $number[0:i]$ 和后缀 $number[i+1:]$ 拼接起来,即为移除 $number[i]$ 后的结果。我们取所有可能的结果中最大的即可。
我们可以枚举字符串 $\textit{number}$ 的所有位置 $\textit{i}$,如果 $\textit{number}[i] = \textit{digit}$,那么我们取 $\textit{number}$ 的前缀 $\textit{number}[0:i]$ 和后缀 $\textit{number}[i+1:]$ 拼接起来,即为移除 $\textit{number}[i]$ 后的结果。我们取所有可能的结果中最大的即可。

时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $number$ 的长度。
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $\textit{number}$ 的长度。

<!-- tabs:start -->

Expand Down Expand Up @@ -195,11 +195,11 @@ class Solution {

### 方法二:贪心

我们可以枚举字符串 $number$ 的所有位置 $i$,如果 $number[i] = digit$,记录 $digit$ 最后一次出现的位置 $last$,并且如果 $i + 1 \lt n$ 且 $number[i] \lt number[i + 1]$,那么我们可以直接返回 $number[0:i] + number[i+1:]$,即为移除 $number[i]$ 后的结果。这是因为如果 $number[i] < number[i + 1]$,那么移除 $number[i]$ 后,结果一定会更大。
我们可以枚举字符串 $\textit{number}$ 的所有位置 $\textit{i}$,如果 $\textit{number}[i] = \textit{digit}$,记录 $\textit{digit}$ 最后一次出现的位置 $\textit{last}$,并且如果 $\textit{i} + 1 < \textit{n}$ 且 $\textit{number}[i] < \textit{number}[i + 1]$,那么我们可以直接返回 $\textit{number}[0:i] + \textit{number}[i+1:]$,即为移除 $\textit{number}[i]$ 后的结果。这是因为如果 $\textit{number}[i] < \textit{number}[i + 1]$,那么移除 $\textit{number}[i]$ 后,结果一定会更大。

遍历结束,我们返回 $number[0:last] + number[last+1:]$ 即可。
遍历结束,我们返回 $\textit{number}[0:\textit{last}] + \textit{number}[\textit{last}+1:]$ 即可。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $number$ 的长度。
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $\textit{number}$ 的长度。

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ Both result in the string &quot;51&quot;.

<!-- solution:start -->

### Solution 1
### Solution 1: Brute Force Enumeration

We can enumerate all positions $\textit{i}$ in the string $\textit{number}$. If $\textit{number}[i] = \textit{digit}$, we take the prefix $\textit{number}[0:i]$ and the suffix $\textit{number}[i+1:]$ of $\textit{number}$ and concatenate them. This gives the result after removing $\textit{number}[i]$. We then take the maximum of all possible results.

The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string $\textit{number}$.

<!-- tabs:start -->

Expand Down Expand Up @@ -187,7 +191,11 @@ class Solution {

<!-- solution:start -->

### Solution 2
### Solution 2: Greedy

We can enumerate all positions $\textit{i}$ in the string $\textit{number}$. If $\textit{number}[i] = \textit{digit}$, we record the last occurrence position of $\textit{digit}$ as $\textit{last}$. If $\textit{i} + 1 < \textit{n}$ and $\textit{number}[i] < \textit{number}[i + 1]$, then we can directly return $\textit{number}[0:i] + \textit{number}[i+1:]$ as the result after removing $\textit{number}[i]$. This is because if $\textit{number}[i] < \textit{number}[i + 1]$, removing $\textit{number}[i]$ will result in a larger number.

After the traversal, we return $\textit{number}[0:\textit{last}] + \textit{number}[\textit{last}+1:]$.

<!-- tabs:start -->

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

我们先建立一个有向图 $\textit{g}$,其中 $\textit{g}[i]$ 表示从城市 $i$ 出发可以到达的城市列表,初始时,每个城市 $i$ 都有一条单向道路通往城市 $i + 1$。

然后,我们对每个查询 $[u, v]$,将 $u$ 添加到 $v$ 的出发城市列表中,然后使用 BFS 求出从城市 $0$ 到城市 $n - 1$ 的最短路径长度,将结果添加到答案数组中。
然后,我们对每个查询 $[u, v]$,将 $v$ 添加到 $u$ 的可达城市列表中,然后使用 BFS 求出从城市 $0$ 到城市 $n - 1$ 的最短路径长度,将结果添加到答案数组中。

最后返回答案数组即可。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ tags:

### Solution 1: BFS

First, we establish a directed graph $\textit{g}$, where $\textit{g}[i]$ represents the list of cities that can be reached from city $i$. Initially, each city $i$ has a one-way road leading to city $i + 1$.
We first build a directed graph $\textit{g}$, where $\textit{g}[i]$ represents the list of cities that can be reached from city $i$. Initially, each city $i$ has a one-way road to city $i + 1$.

Then, for each query $[u, v]$, we add $u$ to the departure city list of $v$, and then use BFS to find the shortest path length from city $0$ to city $n - 1$, adding the result to the answer array.
Then, for each query $[u, v]$, we add $v$ to the list of reachable cities from $u$, and then use BFS to find the shortest path length from city $0$ to city $n - 1$, adding the result to the answer array.

Finally, we return the answer array.

Time complexity is $O(q \times (n + q))$, and space complexity is $O(n + q)$. Here, $n$ and $q$ are the number of cities and the number of queries, respectively.
The time complexity is $O(q \times (n + q))$, and the space complexity is $O(n + q)$. Here, $n$ and $q$ are the number of cities and the number of queries, respectively.

<!-- tabs:start -->

Expand Down
Loading