You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/1300-1399/1326.Minimum Number of Taps to Open to Water a Garden/README_EN.md
+26-1
Original file line number
Diff line number
Diff line change
@@ -66,7 +66,32 @@ Opening Only the second tap will water the whole garden [0,5]
66
66
67
67
<!-- solution:start -->
68
68
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)
Copy file name to clipboardexpand all lines: solution/1300-1399/1328.Break a Palindrome/README_EN.md
+7-1
Original file line number
Diff line number
Diff line change
@@ -57,7 +57,13 @@ Of all the ways, "aaccba" is the lexicographically smallest.
57
57
58
58
<!-- solution:start -->
59
59
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.
According to the problem description, we need to find the maximum value of the array $\sum_{i=0}^{n-2} |a_i - a_{i+1}|$ when reversing a subarray once.
63
+
64
+
Next, we discuss the following cases:
65
+
66
+
1. Do not reverse the subarray.
67
+
2. Reverse the subarray, and the subarray "includes" the first element.
68
+
3. Reverse the subarray, and the subarray "includes" the last element.
69
+
4. Reverse the subarray, and the subarray "does not include" the first and last elements.
70
+
71
+
Let $s$ be the array value when the subarray is not reversed, then $s = \sum_{i=0}^{n-2} |a_i - a_{i+1}|$. We can initialize the answer $ans$ to $s$.
72
+
73
+
If we reverse the subarray and the subarray includes the first element, we can enumerate the last element $a_i$ of the reversed subarray, where $0 \leq i < n-1$. In this case, $ans = \max(ans, s + |a_0 - a_{i+1}| - |a_i - a_{i+1}|)$.
Similarly, if we reverse the subarray and the subarray includes the last element, we can enumerate the first element $a_{i+1}$ of the reversed subarray, where $0 \leq i < n-1$. In this case, $ans = \max(ans, s + |a_{n-1} - a_i| - |a_i - a_{i+1}|)$.
If we reverse the subarray and the subarray does not include the first and last elements, we consider any two adjacent elements in the array as a point pair $(x, y)$. Let the first element of the reversed subarray be $y_1$, and its left adjacent element be $x_1$; let the last element of the reversed subarray be $x_2$, and its right adjacent element be $y_2$.
At this time, compared to not reversing the subarray, the change in the array value is $|x_1 - x_2| + |y_1 - y_2| - |x_1 - y_1| - |x_2 - y_2|$, where the first two terms can be expressed as:
Therefore, we only need to find the maximum value $mx$ of $k_1 \times x + k_2 \times y$, where $k_1, k_2 \in \{-1, 1\}$, and the corresponding minimum value $mi$ of $|x - y|$. Then the maximum change in the array value is $mx - mi$. The answer is $ans = \max(ans, s + \max(0, mx - mi))$.
98
+
99
+
In the code implementation, we define an array of length 5, $dirs=[1, -1, -1, 1, 1]$. Each time we take two adjacent elements of the array as the values of $k_1$ and $k_2$, which can cover all cases of $k_1, k_2 \in \{-1, 1\}$.
100
+
101
+
The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.
102
+
103
+
Similar problems:
104
+
105
+
-[1131. Maximum of Absolute Value Expression](https://github.com/doocs/leetcode/blob/main/solution/1100-1199/1131.Maximum%20of%20Absolute%20Value%20Expression/README_EN.md)
Copy file name to clipboardexpand all lines: solution/1300-1399/1331.Rank Transform of an Array/README_EN.md
+7-1
Original file line number
Diff line number
Diff line change
@@ -67,7 +67,13 @@ tags:
67
67
68
68
<!-- solution:start -->
69
69
70
-
### Solution 1
70
+
### Solution 1: Discretization
71
+
72
+
First, we copy an array $t$, then sort and deduplicate it to obtain an array of length $m$ that is strictly monotonically increasing.
73
+
74
+
Next, we traverse the original array $arr$. For each element $x$ in the array, we use binary search to find the position of $x$ in $t$. The position plus one is the rank of $x$.
75
+
76
+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $arr$.
The Binary Indexed Tree (BIT), also known as the Fenwick Tree, efficiently supports the following two operations:
178
+
179
+
1.**Point Update**`update(x, delta)`: Adds a value `delta` to the element at position `x` in the sequence.
180
+
2.**Prefix Sum Query**`query(x)`: Queries the sum of the sequence over the interval `[1,...,x]`, i.e., the prefix sum at position `x`.
181
+
182
+
Both operations have a time complexity of $O(\log n)$.
183
+
184
+
The fundamental functionality of the Binary Indexed Tree is to count the number of elements smaller than a given element `x`. This comparison is abstract and can refer to size, coordinate, mass, etc.
185
+
186
+
For example, given the array `a[5] = {2, 5, 3, 4, 1}`, the task is to compute `b[i] = the number of elements to the left of position i that are less than or equal to a[i]`. For this example, `b[5] = {0, 1, 1, 2, 0}`.
187
+
188
+
The solution is to traverse the array, first calculating `query(a[i])` for each position, and then updating the Binary Indexed Tree with `update(a[i], 1)`. When the range of numbers is large, discretization is necessary, which involves removing duplicates, sorting, and then assigning an index to each number.
0 commit comments