Skip to content

Commit 48de267

Browse files
committed
fix: update
1 parent c9660bd commit 48de267

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

solution/1300-1399/1326.Minimum Number of Taps to Open to Water a Garden/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ tags:
7575

7676
我们注意到,对于所有能覆盖某个左端点的水龙头,选择能覆盖最远右端点的那个水龙头是最优的。
7777

78-
因此,我们可以先预处理数组 $ranges$,对于第 $i$ 个水龙头,它能覆盖的左端点 $l = max(0, i - ranges[i])$,右端点 $r = i + ranges[i]$,我们算出所有能覆盖左端点 $l$ 的水龙头中,右端点最大的那个位置,记录在数组 $last[i]$ 中。
78+
因此,我们可以先预处理数组 $ranges$,对于第 $i$ 个水龙头,它能覆盖的左端点 $l = \max(0, i - ranges[i])$,右端点 $r = i + ranges[i]$,我们算出所有能覆盖左端点 $l$ 的水龙头中,右端点最大的那个位置,记录在数组 $last[i]$ 中。
7979

8080
然后我们定义以下三个变量,其中:
8181

8282
- 变量 $ans$ 表示最终答案,即最少水龙头数目;
8383
- 变量 $mx$ 表示当前能覆盖的最远右端点;
8484
- 变量 $pre$ 表示上一个水龙头覆盖的最远右端点。
8585

86-
我们在 $[0,...n-1]$ 的范围内遍历所有位置,对于当前位置 $i$,我们用 $last[i]$ 更新 $mx$,即 $mx = max(mx, last[i])$。
86+
我们在 $[0,...n-1]$ 的范围内遍历所有位置,对于当前位置 $i$,我们用 $last[i]$ 更新 $mx$,即 $mx = \max(mx, last[i])$。
8787

8888
- 如果 $mx \leq i$,说明无法覆盖下一个位置,返回 $-1$。
8989
- 如果 $pre = i$,说明需要使用一个新的子区间,因此我们将 $ans$ 加 $1$,并且更新 $pre = mx$。

solution/1300-1399/1326.Minimum Number of Taps to Open to Water a Garden/README_EN.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,32 @@ Opening Only the second tap will water the whole garden [0,5]
6666

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

69-
### Solution 1
69+
### Solution 1: Greedy
70+
71+
We note that for all taps that can cover a certain left endpoint, choosing the tap that can cover the farthest right endpoint is optimal.
72+
73+
Therefore, we can preprocess the array $ranges$. For the $i$-th tap, it can cover the left endpoint $l = \max(0, i - ranges[i])$ and the right endpoint $r = i + ranges[i]$. We calculate the position of the tap that can cover the left endpoint $l$ with the farthest right endpoint and record it in the array $last[i]$.
74+
75+
Then we define the following three variables:
76+
77+
- Variable $ans$ represents the final answer, i.e., the minimum number of taps;
78+
- Variable $mx$ represents the farthest right endpoint that can currently be covered;
79+
- Variable $pre$ represents the farthest right endpoint covered by the previous tap.
80+
81+
We traverse all positions in the range $[0, \ldots, n-1]$. For the current position $i$, we use $last[i]$ to update $mx$, i.e., $mx = \max(mx, last[i])$.
82+
83+
- If $mx \leq i$, it means the next position cannot be covered, so we return $-1$.
84+
- If $pre = i$, it means a new subinterval needs to be used, so we increment $ans$ by $1$ and update $pre = mx$.
85+
86+
After the traversal, we return $ans$.
87+
88+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the garden.
89+
90+
Similar problems:
91+
92+
- [45. Jump Game II](https://github.com/doocs/leetcode/blob/main/solution/0000-0099/0045.Jump%20Game%20II/README.md)
93+
- [55. Jump Game](https://github.com/doocs/leetcode/blob/main/solution/0000-0099/0055.Jump%20Game/README.md)
94+
- [1024. Video Stitching](https://github.com/doocs/leetcode/blob/main/solution/1000-1099/1024.Video%20Stitching/README.md)
7095

7196
<!-- tabs:start -->
7297

solution/1300-1399/1328.Break a Palindrome/README_EN.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ Of all the ways, &quot;aaccba&quot; is the lexicographically smallest.
5757

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

60-
### Solution 1
60+
### Solution 1: Greedy
61+
62+
First, we check if the length of the string is $1$. If it is, we directly return an empty string.
63+
64+
Otherwise, we traverse the first half of the string from left to right, find the first character that is not `'a'`, and change it to `'a'`. If no such character exists, we change the last character to `'b'`.
65+
66+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string.
6167

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

0 commit comments

Comments
 (0)