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.

Input: fruits =[[0,3],[6,4],[8,5]], startPos =3, k =2Output: 0Explanation:
You can move at most k =2 steps and cannot reach any position with fruits.
The key idea is to maximize the total fruits collected by considering all possible intervals you can reach within k steps, either by going left first then right, or right first then left. Since the positions are sorted, we can use prefix sums and sliding window to efficiently compute the sum of fruits in any interval.
classSolution {
public:int maxTotalFruits(vector<vector<int>>& fruits, int startPos, int k) {
int n = fruits.size(), ans =0, l =0, r =0, sum =0;
while (r < n && fruits[r][0] <= startPos + k) ++r;
for (int i =0; i < r; ++i) sum += fruits[i][1];
int left =0;
for (int i =0; i < r; ++i) {
while (fruits[i][0] < startPos - k) sum -= fruits[left++][1];
int minSteps = min(abs(startPos - fruits[i][0]), abs(startPos - fruits[r-1][0])) + fruits[r-1][0] - fruits[i][0];
if (minSteps <= k) ans = max(ans, sum);
}
return ans;
}
};
funcmaxTotalFruits(fruits [][]int, startPosint, kint) int {
n, ans, l, sum:= len(fruits), 0, 0, 0forr:=0; r < n&&fruits[r][0] <=startPos+k; r++ {
sum+=fruits[r][1]
}
left:=0fori:=0; i < n&&fruits[i][0] <=startPos+k; i++ {
forfruits[i][0] < startPos-k {
sum-=fruits[left][1]
left++ }
minSteps:= min(abs(startPos-fruits[i][0]), abs(startPos-fruits[n-1][0])) +fruits[n-1][0] -fruits[i][0]
ifminSteps<=k {
ans = max(ans, sum)
}
}
returnans}
funcabs(xint) int { ifx < 0 { return-x }; returnx }
func min(a, bint) int { ifa < b { returna }; returnb }
func max(a, bint) int { ifa > b { returna }; returnb }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
publicintmaxTotalFruits(int[][] fruits, int startPos, int k) {
int n = fruits.length, ans = 0, l = 0, sum = 0;
int r = 0;
while (r < n && fruits[r][0]<= startPos + k) ++r;
for (int i = 0; i < r; ++i) sum += fruits[i][1];
int left = 0;
for (int i = 0; i < r; ++i) {
while (fruits[i][0]< startPos - k) sum -= fruits[left++][1];
int minSteps = Math.min(Math.abs(startPos - fruits[i][0]), Math.abs(startPos - fruits[r-1][0])) + fruits[r-1][0]- fruits[i][0];
if (minSteps <= k) ans = Math.max(ans, sum);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
funmaxTotalFruits(fruits: Array<IntArray>, startPos: Int, k: Int): Int {
var ans = 0var l = 0var sum = 0var r = 0while (r < fruits.size && fruits[r][0] <= startPos + k) r++for (i in0 until r) sum += fruits[i][1]
var left = 0for (i in0 until r) {
while (fruits[i][0] < startPos - k) { sum -= fruits[left++][1] }
val minSteps = minOf(kotlin.math.abs(startPos - fruits[i][0]), kotlin.math.abs(startPos - fruits[r-1][0])) + fruits[r-1][0] - fruits[i][0]
if (minSteps <= k) ans = maxOf(ans, sum)
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution:
defmaxTotalFruits(self, fruits: list[list[int]], startPos: int, k: int) -> int:
n = len(fruits)
ans = l = sum_ =0 r =0while r < n and fruits[r][0] <= startPos + k:
r +=1for i in range(r):
sum_ += fruits[i][1]
left =0for i in range(r):
while fruits[i][0] < startPos - k:
sum_ -= fruits[left][1]
left +=1 min_steps = min(abs(startPos - fruits[i][0]), abs(startPos - fruits[r-1][0])) + fruits[r-1][0] - fruits[i][0]
if min_steps <= k:
ans = max(ans, sum_)
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
impl Solution {
pubfnmax_total_fruits(fruits: Vec<Vec<i32>>, start_pos: i32, k: i32) -> i32 {
let n = fruits.len();
letmut ans =0;
letmut l =0;
letmut sum =0;
letmut r =0;
while r < n && fruits[r][0] <= start_pos + k { r +=1; }
for i in0..r { sum += fruits[i][1]; }
letmut left =0;
for i in0..r {
while fruits[i][0] < start_pos - k { sum -= fruits[left][1]; left +=1; }
let min_steps = (start_pos - fruits[i][0]).abs().min((start_pos - fruits[r-1][0]).abs()) + fruits[r-1][0] - fruits[i][0];
if min_steps <= k { ans = ans.max(sum); }
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
maxTotalFruits(fruits: number[][], startPos: number, k: number):number {
letn=fruits.length, ans=0, l=0, sum=0, r=0;
while (r<n&&fruits[r][0] <=startPos+k) ++r;
for (leti=0; i<r; ++i) sum+=fruits[i][1];
letleft=0;
for (leti=0; i<r; ++i) {
while (fruits[i][0] <startPos-k) sum-=fruits[left++][1];
letminSteps= Math.min(Math.abs(startPos-fruits[i][0]), Math.abs(startPos-fruits[r-1][0])) +fruits[r-1][0] -fruits[i][0];
if (minSteps<=k) ans= Math.max(ans, sum);
}
returnans;
}
}