2106. Maximum Fruits Harvested After at Most K Steps
UnknownView on LeetCode
Time: O(n)
Space: O(1)
Problem Overview
Maximum Fruits Harvested After at Most K Steps (Unknown) asks you to solve a structured algorithmic task. This is a common Array / Binary Search pattern in coding interviews. Sliding window — try all windows anchored left or right of startPos within reach k.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
Sliding window — try all windows anchored left or right of startPos within reach k.
Related patterns: Array, Binary Search, Sliding Window
2106.cs
C#
// Approach: Sliding window — try all windows anchored left or right of startPos within reach k.
// Time: O(n) Space: O(1)
public class Solution
{
public int MaxTotalFruits(int[][] fruits, int startPos, int k)
{
int maxFruits = 0; // Stores the maximum number of fruits we can collect
int currentSum = 0; // Stores the current sum of fruits between two points
// Two pointers approach: i is for the start point and j is for the end point
for (int i = 0, j = 0; j < fruits.Length; ++j)
{
int positionJ = fruits[j][0]; // The position of the j-th tree
int fruitsAtJ = fruits[j][1]; // The number of fruits at the j-th tree
currentSum += fruitsAtJ; // Add fruits at the j-th tree to the current sum
// Adjust the starting point i to not exceed the maximum distance k
while (i <= j &&
positionJ - fruits[i][0] +
Math.Min(Math.Abs(startPos - fruits[i][0]), Math.Abs(startPos - positionJ))
> k)
{
// Subtract the number of fruits at the i-th tree as we move the start point forward
currentSum -= fruits[i][1];
i++; // Increment the start point
}
// Update maxFruits with the maximum of current sum and previously calculated maxFruits
maxFruits = Math.Max(maxFruits, currentSum);
}
return maxFruits; // Return the maximum number of fruits that can be collected
}
}Was this solution helpful?
Related Problems
- 2302. Count Subarrays With Score Less Than K(Hard)
- 3346. Maximum Frequency of an Element After Performing Operations I(Medium)
- 3347. Maximum Frequency of an Element After Performing Operations II(Unknown)
- 862. Shortest Subarray with Sum at Least K(Hard)
- 1248. Count Number of Nice Subarrays(Medium)
- 1894. Find the Student that Will Replace the Chalk(Unknown)