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/2100-2199/2106.Maximum Fruits Harvested After at Most K Steps/README_EN.md
+49-3Lines changed: 49 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -33,7 +33,7 @@ tags:
33
33
<pre>
34
34
<strong>Input:</strong> fruits = [[2,8],[6,3],[8,6]], startPos = 5, k = 4
35
35
<strong>Output:</strong> 9
36
-
<strong>Explanation:</strong>
36
+
<strong>Explanation:</strong>
37
37
The optimal way is to:
38
38
- Move right to position 6 and harvest 3 fruits
39
39
- Move right to position 8 and harvest 6 fruits
@@ -45,7 +45,7 @@ You moved 3 steps and harvested 3 + 6 = 9 fruits in total.
45
45
<pre>
46
46
<strong>Input:</strong> fruits = [[0,9],[4,1],[5,7],[6,2],[7,4],[10,9]], startPos = 5, k = 4
47
47
<strong>Output:</strong> 14
48
-
<strong>Explanation:</strong>
48
+
<strong>Explanation:</strong>
49
49
You can move at most k = 4 steps, so you cannot reach position 0 nor 10.
50
50
The optimal way is to:
51
51
- Harvest the 7 fruits at the starting position 5
@@ -82,7 +82,30 @@ You can move at most k = 2 steps and cannot reach any position with fruits.
82
82
83
83
<!-- solution:start -->
84
84
85
-
### Solution 1
85
+
### Solution 1: Two Pointers
86
+
87
+
Let's assume the movement range is $[l, r]$ and the starting position is $\textit{startPos}$. We need to calculate the minimum number of steps required. Based on the position of $\textit{startPos}$, we can divide this into three cases:
88
+
89
+
1. If $\textit{startPos} \leq l$, then we move right from $\textit{startPos}$ to $r$. The minimum number of steps is $r - \textit{startPos}$;
90
+
2. If $\textit{startPos} \geq r$, then we move left from $\textit{startPos}$ to $l$. The minimum number of steps is $\textit{startPos} - l$;
91
+
3. If $l < \textit{startPos} < r$, we can either move left from $\textit{startPos}$ to $l$ then right to $r$, or move right from $\textit{startPos}$ to $r$ then left to $l$. The minimum number of steps is $r - l + \min(\lvert \textit{startPos} - l \rvert, \lvert r - \textit{startPos} \rvert)$.
92
+
93
+
All three cases can be unified by the formula $r - l + \min(\lvert \textit{startPos} - l \rvert, \lvert r - \textit{startPos} \rvert)$.
94
+
95
+
Suppose we fix the right endpoint $r$ of the interval and move the left endpoint $l$ to the right. Let's see how the minimum number of steps changes:
96
+
97
+
1. If $\textit{startPos} \leq l$, as $l$ increases, the minimum number of steps remains unchanged.
98
+
2. If $\textit{startPos} > l$, as $l$ increases, the minimum number of steps decreases.
99
+
100
+
Therefore, as $l$ increases, the minimum number of steps is non-strictly monotonically decreasing. Based on this, we can use the two-pointer approach to find all qualifying maximum intervals, then take the one with the maximum total fruits among all qualifying intervals as the answer.
101
+
102
+
Specifically, we use two pointers $i$ and $j$ to point to the left and right indices of the interval, initially $i = j = 0$. We also use a variable $s$ to record the total number of fruits in the interval, initially $s = 0$.
103
+
104
+
Each time we include $j$ in the interval, then update $s = s + \textit{fruits}[j][1]$. If the minimum number of steps in the current interval $\textit{fruits}[j][0] - \textit{fruits}[i][0] + \min(\lvert \textit{startPos} - \textit{fruits}[i][0] \rvert, \lvert \textit{startPos} - \textit{fruits}[j][0] \rvert)$ is greater than $k$, we move $i$ to the right in a loop until $i > j$ or the minimum number of steps in the interval is less than or equal to $k$. At this point, we update the answer $\textit{ans} = \max(\textit{ans}, s)$. Continue moving $j$ until $j$ reaches the end of the array.
105
+
106
+
Finally, return the answer.
107
+
108
+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
86
109
87
110
<!-- tabs:start -->
88
111
@@ -197,6 +220,29 @@ function maxTotalFruits(fruits: number[][], startPos: number, k: number): number
0 commit comments