Fruits are available at some positions on an infinite x-axis. You are given a 2D integer array fruits where fruits[i] = [positioni, amounti] depicts
amounti fruits at the position positioni. fruits is already sorted by positioni in ascending order , and each positioni 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 mostk 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 themaximum total number of fruits you can harvest.
Input: fruits =[[2,8],[6,3],[8,6]], startPos =5, k =4Output: 9Explanation:
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.
Input: fruits =[[0,9],[4,1],[5,7],[6,2],[7,4],[10,9]], startPos =5, k =4Output: 14Explanation:
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.
The key insight is to use a sliding window approach to find all possible contiguous subarrays of fruits that can be reached within k steps. For any subarray from left to right, we have two strategies: go left first then sweep right, or go right first then sweep left. We choose the strategy that requires fewer steps.
classSolution {
public:int maxTotalFruits(vector<vector<int>>& fruits, int startPos, int k) {
int n = fruits.size(), ans =0, sum =0;
int left =0, right =0;
while (right < n) {
sum += fruits[right][1];
while (left <= right && getSteps(fruits, startPos, left, right) > k) {
sum -= fruits[left][1];
left++;
}
ans = max(ans, sum);
right++;
}
return ans;
}
private:int getSteps(vector<vector<int>>& fruits, int startPos, int left, int right) {
returnmin(abs(startPos - fruits[left][0]),
abs(startPos - fruits[right][0])) + fruits[right][0] - fruits[left][0];
}
};
classSolution {
publicintmaxTotalFruits(int[][] fruits, int startPos, int k) {
int n = fruits.length, ans = 0, sum = 0;
int left = 0, right = 0;
// Sliding window approachwhile (right < n) {
sum += fruits[right][1];
// Check if current window is validwhile (left <= right && getSteps(fruits, startPos, left, right) > k) {
sum -= fruits[left][1];
left++;
}
ans = Math.max(ans, sum);
right++;
}
return ans;
}
privateintgetSteps(int[][] fruits, int startPos, int left, int right) {
return Math.min(Math.abs(startPos - fruits[left][0]),
Math.abs(startPos - fruits[right][0])) + fruits[right][0]- fruits[left][0];
}
}
impl Solution {
pubfnmax_total_fruits(fruits: Vec<Vec<i32>>, start_pos: i32, k: i32) -> i32 {
let n = fruits.len();
letmut ans =0;
letmut sum =0;
letmut left =0;
letmut right =0;
while right < n {
sum += fruits[right][1];
while left <= right && Self::get_steps(&fruits, start_pos, left, right) > k {
sum -= fruits[left][1];
left +=1;
}
ans = ans.max(sum);
right +=1;
}
ans
}
fnget_steps(fruits: &Vec<Vec<i32>>, start_pos: i32, left: usize, right: usize) -> i32 {
(start_pos - fruits[left][0]).abs().min((start_pos - fruits[right][0]).abs()) + fruits[right][0] - fruits[left][0]
}
}
*β° Time complexity: `O(n)`β Each fruit position is visited at most twice (once when expanding right pointer, once when contracting left pointer), making it linear time.*π§Ί Space complexity: `O(1)`β Only using a constant amount of extra variables (left, right, sum, ans) regardless of input size.