Maximum Number of Eaten Apples - Problem
Imagine owning a magical apple tree that grows fresh apples daily for
Here's how it works: On day
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
โข Some days the tree might not produce any apples (
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!
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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code