Maximum Fruits Harvested After at Most K Steps - Problem

Imagine you're a fruit collector on an infinite horizontal line with fruit trees scattered at various positions. You start at a specific position and have a limited number of steps to collect as many fruits as possible!

You're given:
fruits[i] = [position, amount] - fruit locations and quantities (sorted by position)
startPos - your starting position
k - maximum steps you can take

Goal: Maximize the total fruits collected within k steps. You can move left or right, and each unit of movement costs 1 step. When you reach a position with fruits, you automatically harvest all of them.

Key Challenge: Should you go left first then right? Right first then left? Or stay in one direction? The optimal strategy isn't always obvious!

Input & Output

example_1.py — Basic case
$ Input: fruits = [[2,8],[6,3],[8,6]], startPos = 5, k = 4
Output: 9
💡 Note: Start at position 5 with 4 steps. Go right to position 6 (1 step) and collect 3 fruits, then go to position 8 (2 more steps) and collect 6 fruits. Total: 3 + 6 = 9 fruits using 3 steps.
example_2.py — Left and right movement
$ Input: fruits = [[0,9],[4,1],[5,7],[6,2],[7,4],[10,9]], startPos = 5, k = 4
Output: 14
💡 Note: Start at position 5. Go left to position 4 (1 step) and collect 1 fruit, then return to start (1 step) and go right to positions 6 and 7 (2 steps) collecting 2 + 4 = 6 fruits. Also collect 7 fruits at start position. Total: 1 + 7 + 2 + 4 = 14 fruits.
example_3.py — Edge case with no fruits reachable
$ Input: fruits = [[0,3],[6,4],[8,5]], startPos = 3, k = 2
Output: 0
💡 Note: Start at position 3 with only 2 steps. The nearest fruits are at positions 0 and 6, both requiring at least 3 steps to reach, so no fruits can be collected.

Constraints

  • 1 ≤ fruits.length ≤ 105
  • fruits[i].length = 2
  • 0 ≤ startPos, positioni ≤ 2 × 108
  • positioni-1 < positioni (sorted by position)
  • 1 ≤ amounti ≤ 104
  • 0 ≤ k ≤ 2 × 105
  • Large coordinate space but sparse fruit distribution

Visualization

Tap to expand
Maximum Fruits Harvested After at Most K Steps INPUT 2 5 6 8 8 3 6 START fruits = [[2,8],[6,3],[8,6]] startPos = 5 k = 4 = Fruit tree (amount) = Starting position Pos 2: 8 fruits Pos 6: 3 fruits Pos 8: 6 fruits Max steps: 4 ALGORITHM STEPS 1 Sliding Window Try all left/right combinations 2 Check Reachability Can reach within k steps? 3 Calculate Steps min(2*left+right, 2*right+left) 4 Track Maximum Keep best fruit sum found Path Analysis Right only: 5-->6-->8-->9 4 steps, reaches pos 9 Collects: 3+6 = 9 [OK] Left first: needs 6+ steps to reach pos 2 and back FINAL RESULT 5 6 8 3 6 Output: 9 Calculation Pos 6: +3 fruits Pos 8: +6 fruits Total: 3 + 6 = 9 Steps used: 4 (5-->6-->8) Optimal path: Right only Key Insight: The optimal strategy uses a sliding window approach. For each possible window of fruits, calculate the minimum steps needed: either go left first then right (2*left + right) or right first then left (2*right + left). Here, going right only to positions 6 and 8 takes exactly 4 steps and yields maximum 9 fruits. TutorialsPoint - Maximum Fruits Harvested After at Most K Steps | Optimal Solution
Asked in
Google 34 Amazon 28 Microsoft 19 Meta 12
43.6K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen