Maximum Number of Eaten Apples - Problem
Imagine owning a magical apple tree that grows fresh apples daily for n consecutive days! ๐ŸŒณ๐ŸŽ

Here's how it works: On day i, your tree produces apples[i] fresh apples, but there's a catch - these apples will rot after exactly days[i] days. This means apples grown on day i must be consumed by day i + days[i] - 1 (inclusive), or they become inedible.

Your goal: Eat the maximum number of apples possible following these rules:
โ€ข You can eat at most 1 apple per day (doctor's orders!) ๐Ÿ‘ฉโ€โš•๏ธ
โ€ข You can continue eating even after the tree stops producing (beyond day n)
โ€ข Some days the tree might not produce any apples (apples[i] = 0 and days[i] = 0)

Strategy is key: Should you eat the freshest apples first, or save the longer-lasting ones for later? The choice determines how many apples you can enjoy before they spoil!

Input & Output

example_1.py โ€” Basic Case
$ Input: apples = [1,2,3,5,2], days = [3,2,1,4,2]
โ€บ Output: 7
๐Ÿ’ก Note: Day 0: eat 1 apple from day 0 batch (expires day 3). Day 1: eat 1 apple from day 2 batch (expires day 2). Day 2: eat 1 apple from day 1 batch (expires day 3). Day 3: eat 1 apple from day 0 batch (expires day 3). Day 4: eat 1 apple from day 3 batch (expires day 7). Day 5: eat 1 apple from day 4 batch (expires day 6). Day 6: eat 1 apple from day 4 batch (expires day 6). Total: 7 apples.
example_2.py โ€” Single Apple
$ Input: apples = [3,0,0,0,0,2], days = [3,0,0,0,0,2]
โ€บ Output: 5
๐Ÿ’ก Note: Day 0-2: eat the 3 apples from day 0 (expire day 3). Days 3-4: no apples available. Day 5-6: eat the 2 apples from day 5 (expire day 7). Total: 5 apples.
example_3.py โ€” Edge Case
$ Input: apples = [1,10,17,10,12,6,2,12,3,2,2,5,16,2,9,11,15], days = [8,19,1,1,18,20,9,8,8,7,14,14,15,14,15,3,15]
โ€บ Output: 71
๐Ÿ’ก Note: This complex case requires careful prioritization of apples by expiration date to maximize consumption before spoilage occurs.

Constraints

  • n == apples.length == days.length
  • 1 โ‰ค n โ‰ค 2 ร— 104
  • 0 โ‰ค apples[i] โ‰ค 2 ร— 104
  • 0 โ‰ค days[i] โ‰ค 2 ร— 104
  • If apples[i] == 0, then days[i] == 0

Visualization

Tap to expand
๐ŸŽ Smart Apple Orchard Management๐ŸŒณ Apple Tree Production Schedule๐ŸŒณDay 0: 1๐ŸŽ๐ŸŒณDay 1: 2๐ŸŽ๐ŸŒณDay 2: 3๐ŸŽ๐ŸŒณDay 3: 5๐ŸŽ๐ŸŒณDay 4: 2๐ŸŽ๐Ÿ“‹ Priority Queue: Organized by Expiration DateURGENTExpires Day 23 apples leftExpires Day 31 apple leftExpires Day 62 apples leftExpires Day 75 apples leftStrategy:1. Always eat from leftmost (urgent) box2. Reorganize when box becomes empty๐Ÿ—“๏ธ Daily Routine ProcessMorningAdd new applesto priority queueCleanupRemove expiredapples (rotten)Eat SmartTake 1 apple frommost urgent batchUpdateReorganize queuefor next day๐ŸŽฏ Result: Maximum apples eaten with zero waste!
Understanding the Visualization
1
Receive Daily Delivery
Each morning, receive new apples with specific expiration dates
2
Organize by Priority
Arrange apples by expiration date - soonest to expire at front
3
Remove Spoiled Items
Throw away any apples that have already expired
4
Eat/Sell Most Urgent
Choose one apple from the batch expiring soonest
5
Update Inventory
Reorganize remaining apples and repeat tomorrow
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach of always eating apples that expire soonest guarantees maximum consumption because it prevents early spoilage and makes room for future apples.
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
28.5K Views
Medium-High Frequency
~25 min Avg. Time
980 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