↓Skip to main content
  1. LeetCode/

LeetCode 2106: Maximum Fruits Harvested After at Most K Steps

·2 mins· ·
LeetCode Hard Sliding-Window Prefix-Sum Binary-Search Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: hard First Attempt: 2026-01-12

  • Submissions: Wrong Answer → Accepted ×2

Approach
#

The walked range is always a contiguous interval [L, R], and the harvest is every fruit in it.

The fewest steps to cover [L, R]: go to one side first, then turn back to the other,

steps = (R - L) + min(|start - L|, |R - start|)

This also holds when start is outside the interval (for start < L it gives R - start).

Steps never decrease as R grows, so slide a window over the fruits array: add fruits[right] on the right, shrink the left while steps exceed k, and track the largest window total.

My Attempts
#

My version slid over coordinates: from startPos - k to startPos + k, then once more in reverse, each time computing how far the other side could reach after turning back. Attempt 1 shrank only one step with an if when turning back, not far enough. For fruits = [[3, 1], [7, 3]], startPos = 5, k = 5, taking both sides needs 6 steps, so only the 3 on the right can be taken and the answer is 3; this returned 4.

Switching to while passed, but the coordinate range is 2k, slower than sliding over the fruits (1.4 seconds).

Solution
#

class Solution:
    def maxTotalFruits(self, fruits: List[List[int]], startPos: int, k: int) -> int:
        def steps(lo: int, hi: int) -> int:        # fewest steps to cover [lo, hi]
            return (hi - lo) + min(abs(startPos - lo), abs(hi - startPos))

        left = total = best = 0
        for right, (pos, amount) in enumerate(fruits):
            total += amount
            while left <= right and steps(fruits[left][0], pos) > k:
                total -= fruits[left][1]
                left += 1
            best = max(best, total)
        return best

left may pass right (even fruits[right] alone is unreachable); the window is then empty and total is 0.

Complexity
#

Time O(n) for n fruit positions, space O(1).

Mistakes to Avoid
#

  • Considering only “left first” or only “right first”: take the smaller of the two.
  • Sliding over coordinates: the range is 2k, unrelated to the number of fruits.

Interview Explanation
#

The harvest is always a contiguous block of fruits. Covering [L, R] takes the interval length plus the distance from the start to the nearer end, going there first and turning back. That cost never decreases as the right end grows, so I slide a window over the fruits array, shrinking the left when steps exceed k, and track the largest total. Time O(n).

Related

LeetCode 1838: Frequency of the Most Frequent Element
·2 mins
LeetCode Medium Sliding-Window Sorting Greedy Prefix-Sum Binary-Search Array
LeetCode 1838 note: after sorting, raising the whole window to nums[right] costs nums[right] × length − window sum; shrink the left while that exceeds k
LeetCode 2271: Maximum White Tiles Covered by a Carpet
·2 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 2271 note: starting the carpet at some segment’s start is never worse; after sorting, two pointers sum the fully covered segments and count the last one partially
LeetCode 209: Minimum Size Subarray Sum (Sliding Window)
·1 min
LeetCode Daily Medium Array Sliding-Window Two-Pointers Prefix-Sum Binary-Search
Sliding window with two pointers; minimize subarray length where sum ≥ target.
LeetCode 2779: Maximum Beauty of an Array After Applying Operation
·2 mins
LeetCode Medium Sliding-Window Sorting Binary-Search Array
LeetCode 2779 note: each number can become anything in [x - k, x + k], so a group can become one value ⇔ max - min <= 2k; sort and find the longest such window
LeetCode 2831: Find the Longest Equal Subarray
·2 mins
LeetCode Medium Sliding-Window Hash-Table Binary-Search Array
LeetCode 2831 note: list the positions of each value and slide a window over that list; the deletions needed are the span of positions minus the number of copies
LeetCode 220: Contains Duplicate III
·3 mins
LeetCode Hard Sliding-Window Bucket-Sort Ordered-Set Sorting Array
LeetCode 220 note: with bucket size valueDiff + 1, two numbers in one bucket always qualify, so only the two neighboring buckets need checking; the window keeps the latest indexDiff numbers, so each bucket holds at most one