From c64158a83200969ea9263e20e11ecb6013207bcd Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 19 Nov 2024 19:28:47 +0800 Subject: [PATCH] feat: update solutions to lc problems: No.2251,2259 --- .../2251.Number of Flowers in Full Bloom/README.md | 4 ++-- .../README_EN.md | 12 ++++++++++-- .../README.md | 10 +++++----- .../README_EN.md | 12 ++++++++++-- .../README.md | 2 +- .../README_EN.md | 6 +++--- 6 files changed, 31 insertions(+), 15 deletions(-) diff --git a/solution/2200-2299/2251.Number of Flowers in Full Bloom/README.md b/solution/2200-2299/2251.Number of Flowers in Full Bloom/README.md index e1e903ce0ea3d..e133310786247 100644 --- a/solution/2200-2299/2251.Number of Flowers in Full Bloom/README.md +++ b/solution/2200-2299/2251.Number of Flowers in Full Bloom/README.md @@ -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}$ 的长度。 @@ -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}$ 的长度。 diff --git a/solution/2200-2299/2251.Number of Flowers in Full Bloom/README_EN.md b/solution/2200-2299/2251.Number of Flowers in Full Bloom/README_EN.md index 21331834fae49..c7d1db5aa9c04 100644 --- a/solution/2200-2299/2251.Number of Flowers in Full Bloom/README_EN.md +++ b/solution/2200-2299/2251.Number of Flowers in Full Bloom/README_EN.md @@ -63,7 +63,11 @@ For each person, we return the number of flowers in full bloom during their arri -### 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. @@ -255,7 +259,11 @@ impl Solution { -### 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. diff --git a/solution/2200-2299/2259.Remove Digit From Number to Maximize Result/README.md b/solution/2200-2299/2259.Remove Digit From Number to Maximize Result/README.md index 3587558f11aed..d2e615a0abeb8 100644 --- a/solution/2200-2299/2259.Remove Digit From Number to Maximize Result/README.md +++ b/solution/2200-2299/2259.Remove Digit From Number to Maximize Result/README.md @@ -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}$ 的长度。 @@ -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}$ 的长度。 diff --git a/solution/2200-2299/2259.Remove Digit From Number to Maximize Result/README_EN.md b/solution/2200-2299/2259.Remove Digit From Number to Maximize Result/README_EN.md index 519bc5d879f73..c4a77c74ba57d 100644 --- a/solution/2200-2299/2259.Remove Digit From Number to Maximize Result/README_EN.md +++ b/solution/2200-2299/2259.Remove Digit From Number to Maximize Result/README_EN.md @@ -67,7 +67,11 @@ Both result in the string "51". -### 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}$. @@ -187,7 +191,11 @@ class Solution { -### 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:]$. diff --git a/solution/3200-3299/3243.Shortest Distance After Road Addition Queries I/README.md b/solution/3200-3299/3243.Shortest Distance After Road Addition Queries I/README.md index 33bcf625aac61..21ae96cf6b2d5 100644 --- a/solution/3200-3299/3243.Shortest Distance After Road Addition Queries I/README.md +++ b/solution/3200-3299/3243.Shortest Distance After Road Addition Queries I/README.md @@ -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$ 的最短路径长度,将结果添加到答案数组中。 最后返回答案数组即可。 diff --git a/solution/3200-3299/3243.Shortest Distance After Road Addition Queries I/README_EN.md b/solution/3200-3299/3243.Shortest Distance After Road Addition Queries I/README_EN.md index 36e9cc40ff9ec..428ea930765ee 100644 --- a/solution/3200-3299/3243.Shortest Distance After Road Addition Queries I/README_EN.md +++ b/solution/3200-3299/3243.Shortest Distance After Road Addition Queries I/README_EN.md @@ -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.