Maximum Number of Eaten Apples - Problem

There is a special kind of apple tree that grows apples every day for n days. On the ith day, the tree grows apples[i] apples that will rot after days[i] days, that is on day i + days[i] the apples will be rotten and cannot be eaten.

On some days, the apple tree does not grow any apples, which are denoted by apples[i] == 0 and days[i] == 0.

You decided to eat at most one apple a day (to keep the doctors away). Note that you can keep eating after the first n days.

Given two integer arrays days and apples of length n, return the maximum number of apples you can eat.

Input & Output

Example 1 — Basic Case
$ Input: apples = [1,2,3,5], days = [3,2,1,4]
Output: 7
💡 Note: Day 0: eat apple from batch 2 (expires day 2). Day 1: eat apple from batch 1 (expires day 3). Day 2: eat apple from batch 1 (expires day 3). Day 3: eat apple from batch 0 (expires day 3). Day 4: eat apple from batch 3 (expires day 7). Day 5: eat apple from batch 3. Day 6: eat apple from batch 3. Total: 7 apples.
Example 2 — Some Days No Apples
$ Input: apples = [3,0,0,0,0,2], days = [3,0,0,0,0,2]
Output: 5
💡 Note: Days 0-2: eat one apple each day from first batch (3 apples expire day 3). Days 3-4: no apples available. Day 5: add second batch (2 apples expire day 7). Days 5-6: eat from second batch. Total: 5 apples.
Example 3 — Single Day
$ Input: apples = [1], days = [1]
Output: 1
💡 Note: Day 0: eat the single apple (expires day 1). No more apples available after day 0. Total: 1 apple.

Constraints

  • 1 ≤ n ≤ 2 × 104
  • 0 ≤ apples[i] ≤ 2 × 104
  • 1 ≤ days[i] ≤ 2 × 104
  • apples[i] == 0 if and only if days[i] == 0

Visualization

Tap to expand
Maximum Number of Eaten Apples INPUT apples[] array: 1 2 3 5 Index: 0 1 2 3 days[] array: 3 2 1 4 Expiry Days (i + days[i]): 3 3 3 7 Eat at most 1 apple/day Can eat after n days n = 4 days ALGORITHM STEPS 1 Use Min-Heap Store (expiry, count) pairs 2 Each Day Process Add new apples to heap 3 Remove Rotten Pop expired apples 4 Eat Earliest Expiry Greedy: eat soonest rot Min-Heap (by expiry) 3,1 3,2 7,5 (expiry, apples) Sorted by expiry date FINAL RESULT Day-by-Day Eating: Day 0: Eat 1 (exp 3) Day 1: Eat 1 (exp 3) Day 2: Eat 1 (exp 3) Day 3: Eat 1 (exp 7) Day 4: Eat 1 (exp 7) Day 5: Eat 1 (exp 7) Day 6: Eat 1 (exp 7) OUTPUT 7 OK - Maximum apples eaten! Greedy approach optimal Key Insight: The greedy strategy is to always eat the apple that will expire soonest. Using a min-heap ordered by expiration date ensures we can efficiently find and remove the earliest expiring apple each day. Time Complexity: O(n log n) | Space Complexity: O(n) for the priority queue. TutorialsPoint - Maximum Number of Eaten Apples | Greedy with Priority Queue (Min-Heap)
Asked in
Amazon 15 Google 12 Facebook 8 Microsoft 6
34.5K Views
Medium Frequency
~25 min Avg. Time
892 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