Maximum Fruits Harvested After at Most K Steps Problem
Description
LeetCode Problem 2106.
Fruits are available at some positions on an infinite x-axis. You are given a 2D integer array fruits where fruits[i] = [position_i, amount_i] depicts amount_i fruits at the position position_i. fruits is already sorted by position_i in ascending order, and each position_i is unique.
You are also given an integer startPos and an integer k. Initially, you are at the position startPos. From any position, you can either walk to the left or right. It takes one step to move one unit on the x-axis, and you can walk at most k steps in total. For every position you reach, you harvest all the fruits at that position, and the fruits will disappear from that position.
Return the maximum total number of fruits you can harvest.
Example 1:

Input: fruits = [[2,8],[6,3],[8,6]], startPos = 5, k = 4
Output: 9
Explanation:
The optimal way is to:
- Move right to position 6 and harvest 3 fruits
- Move right to position 8 and harvest 6 fruits
You moved 3 steps and harvested 3 + 6 = 9 fruits in total.
Example 2:

Input: fruits = [[0,9],[4,1],[5,7],[6,2],[7,4],[10,9]], startPos = 5, k = 4
Output: 14
Explanation:
You can move at most k = 4 steps, so you cannot reach position 0 nor 10.
The optimal way is to:
- Harvest the 7 fruits at the starting position 5
- Move left to position 4 and harvest 1 fruit
- Move right to position 6 and harvest 2 fruits
- Move right to position 7 and harvest 4 fruits
You moved 1 + 3 = 4 steps and harvested 7 + 1 + 2 + 4 = 14 fruits in total.
Example 3:

Input: fruits = [[0,3],[6,4],[8,5]], startPos = 3, k = 2
Output: 0
Explanation:
You can move at most k = 2 steps and cannot reach any position with fruits.
Constraints:
1 <= fruits.length <= 10^5fruits[i].length == 20 <= startPos, position_i <= 2 * 10^5position_(i-1) < position_ifor anyi > 0(0-indexed)1 <= amount_i <= 10^40 <= k <= 2 * 10^5
Solution
Approach: Two pointers
Suppose we harvest the positions in [l, r]. The fewest steps needed from startPos is:
r - startPosifstartPos <= l(just walk right);startPos - lifstartPos >= r(just walk left);- otherwise go to the nearer end first and then sweep to the other one.
All three cases equal r - l + min(|startPos - l|, |r - startPos|).
For a fixed r, this cost never increases as l moves right, so a sliding window works. Let j be the right end (a fruit index) and i the left end, and keep s, the fruit total inside. After adding fruit j, move i right while the cost of [fruits[i][0], fruits[j][0]] exceeds k, removing those fruits from s. Record the maximum s.
Complexity: O(n) time, O(1) space.
class Solution:
def maxTotalFruits(self, fruits: List[List[int]], startPos: int, k: int) -> int:
ans = i = s = 0
for j, (pj, fj) in enumerate(fruits):
s += fj
while (
i <= j
and pj
- fruits[i][0]
+ min(abs(startPos - fruits[i][0]), abs(startPos - fruits[j][0]))
> k
):
s -= fruits[i][1]
i += 1
ans = max(ans, s)
return ans