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:
•
•
•
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!
You're given:
•
fruits[i] = [position, amount] - fruit locations and quantities (sorted by position)•
startPos - your starting position•
k - maximum steps you can takeGoal: 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
Understanding the Visualization
1
Analyze Patterns
Recognize that optimal paths use left-first or right-first strategies
2
Calculate Costs
Account for return trip costs when switching directions
3
Find Ranges
Use binary search to efficiently find fruit positions in reachable range
4
Sum Efficiently
Use prefix sums for O(1) range sum calculations
Key Takeaway
🎯 Key Insight: The optimal solution recognizes that you only need to consider two movement patterns and uses efficient data structures to quickly evaluate all possibilities within the step budget.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code