Skip to content

Commit f0e6867

Browse files
authored
feat: update solutions to lc problems: No.2251,2259 (#3791)
1 parent 4b72ddb commit f0e6867

File tree

6 files changed

+31
-15
lines changed

6 files changed

+31
-15
lines changed

Diff for: solution/2200-2299/2251.Number of Flowers in Full Bloom/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ tags:
7373

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

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

7878
<!-- tabs:start -->
7979

@@ -269,7 +269,7 @@ impl Solution {
269269

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

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

274274
<!-- tabs:start -->
275275

Diff for: solution/2200-2299/2251.Number of Flowers in Full Bloom/README_EN.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ For each person, we return the number of flowers in full bloom during their arri
6363

6464
<!-- solution:start -->
6565

66-
### Solution 1
66+
### Solution 1: Sorting + Binary Search
67+
68+
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.
69+
70+
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.
6771

6872
<!-- tabs:start -->
6973

@@ -255,7 +259,11 @@ impl Solution {
255259

256260
<!-- solution:start -->
257261

258-
### Solution 2
262+
### Solution 2: Difference Array + Sorting + Offline Query
263+
264+
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.
265+
266+
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.
259267

260268
<!-- tabs:start -->
261269

Diff for: solution/2200-2299/2259.Remove Digit From Number to Maximize Result/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ tags:
7171

7272
### 方法一:暴力枚举
7373

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

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

7878
<!-- tabs:start -->
7979

@@ -195,11 +195,11 @@ class Solution {
195195

196196
### 方法二:贪心
197197

198-
我们可以枚举字符串 $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]$ 后,结果一定会更大。
198+
我们可以枚举字符串 $\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]$ 后,结果一定会更大。
199199

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

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

204204
<!-- tabs:start -->
205205

Diff for: solution/2200-2299/2259.Remove Digit From Number to Maximize Result/README_EN.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ Both result in the string &quot;51&quot;.
6767

6868
<!-- solution:start -->
6969

70-
### Solution 1
70+
### Solution 1: Brute Force Enumeration
71+
72+
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.
73+
74+
The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string $\textit{number}$.
7175

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

@@ -187,7 +191,11 @@ class Solution {
187191

188192
<!-- solution:start -->
189193

190-
### Solution 2
194+
### Solution 2: Greedy
195+
196+
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.
197+
198+
After the traversal, we return $\textit{number}[0:\textit{last}] + \textit{number}[\textit{last}+1:]$.
191199

192200
<!-- tabs:start -->
193201

Diff for: solution/3200-3299/3243.Shortest Distance After Road Addition Queries I/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ tags:
9393

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

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

9898
最后返回答案数组即可。
9999

Diff for: solution/3200-3299/3243.Shortest Distance After Road Addition Queries I/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ tags:
8989

9090
### Solution 1: BFS
9191

92-
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$.
92+
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$.
9393

94-
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.
94+
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.
9595

9696
Finally, we return the answer array.
9797

98-
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.
98+
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.
9999

100100
<!-- tabs:start -->
101101

0 commit comments

Comments
 (0)