Solving Leetcode Interviews in Seconds with AI: Maximum Fruits Harvested After at Most K Steps
Introduction
In this blog post, we will explore how to solve the LeetCode problem "2106" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
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 most k 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 the maximum total number of fruits you can harvest. Example 1: Input: fruits = [[2,8],[6,3],[8,6]], startPos = 5, k = 4 Output: 9 Explanation: 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. Example 2: Input: fruits = [[0,9],[4,1],[5,7],[6,2],[7,4],[10,9]], startPos = 5, k = 4 Output: 14 Explanation: 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. Example 3: Input: fruits = [[0,3],[6,4],[8,5]], startPos = 3, k = 2 Output: 0 Explanation: You can move at most k = 2 steps and cannot reach any position with fruits. Constraints: 1 <= fruits.length <= 105 fruits[i].length == 2 0 <= startPos, positioni <= 2 105 positioni-1 < positioni for any i > 0 (0-indexed) 1 <= amounti <= 104 0 <= k <= 2 105
Explanation
Here's the breakdown of the solution:
- Prefix Sum and Sliding Window: Calculate the prefix sum of fruits to efficiently compute the sum of fruits within a given range. Use a sliding window approach to explore different reachable ranges of fruits within the allowed
ksteps. - Optimization with Two-Way Movement: For each reachable range, consider moving left first and then right, or right first and then left, and choose the strategy that yields the maximum number of fruits. The total cost depends on the furthest left and right positions you reach.
Binary Search (Optional): Can utilize binary search for finding the indexes of fruits within the range in logarithmic time. However, because we iterate in order, the complexity is dominated by the outer loops.
Runtime Complexity: O(N), where N is the number of fruits.
- Storage Complexity: O(N)
Code
def maxTotalFruits(fruits, startPos, k):
n = len(fruits)
prefix_sum = [0] * (n + 1)
for i in range(n):
prefix_sum[i + 1] = prefix_sum[i] + fruits[i][1]
max_fruits = 0
for i in range(n):
# Reach fruits[i] from startPos
left = fruits[i][0]
dist = abs(left - startPos)
if dist > k:
continue
# Option 1: go left to fruits[i][0] and then go right as far as possible
remaining_steps = k - dist
right_bound = left
while right_bound < 2 * 10**5 + 10:
right_idx = -1
low, high = i, n-1
while low <= high:
mid = (low + high) // 2
if fruits[mid][0] <= left + remaining_steps:
right_idx = mid
low = mid + 1
else:
high = mid - 1
if right_idx == -1:
break
right = fruits[right_idx][0]
total_dist = abs(left-startPos) + abs(right-left)
if total_dist <= k:
start_idx = i
total = prefix_sum[right_idx+1] - prefix_sum[start_idx]
max_fruits = max(max_fruits, total)
break # one iteration because right is determined with the maximum remaining steps
# Option 2: go right to fruits[i][0] and then go left as far as possible
right = fruits[i][0]
dist = abs(right - startPos)
if dist > k:
continue
remaining_steps = k - dist
left_bound = right
while left_bound >= 0:
left_idx = -1
low, high = 0, i
while low <= high:
mid = (low + high) // 2
if fruits[mid][0] >= right - remaining_steps:
left_idx = mid
high = mid - 1
else:
low = mid + 1
if left_idx == -1:
break
left = fruits[left_idx][0]
total_dist = abs(right-startPos) + abs(right-left)
if total_dist <= k:
total = prefix_sum[i+1] - prefix_sum[left_idx]
max_fruits = max(max_fruits, total)
break # one iteration because left is determined with the maximum remaining steps
#handle initial position
initial_idx = -1
low, high = 0, n-1
while low <= high:
mid = (low + high) // 2
if fruits[mid][0] == startPos:
initial_idx = mid
break
elif fruits[mid][0] < startPos:
low = mid + 1
else:
high = mid - 1
if initial_idx != -1:
max_fruits = max(max_fruits, fruits[initial_idx][1])
return max_fruits