Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find maximum number of eaten apples in Python
Suppose we have two arrays called apples and days of same length n. There is a special kind of apple tree that grows apples every day for n consecutive days. On the ith day, it grows apples[i] number of apples that will rot after days[i] days. We can take at most one apple a day and want to find the maximum number of apples we can eat.
So, if the input is like apples = [1,2,3,5,2] and days = [3,2,1,4,2], then the output will be 7 because ?
On day 1, we eat an apple that grew on the first day.
On day 2, we eat an apple that grew on the second day.
On day 3, we eat an apple that grew on the second day. After this day, the apples that grew on the third day will rot.
On days 4 to 7, we eat apples that grew on the fourth day.
Algorithm Approach
We use a min-heap to prioritize eating apples that will expire earliest. The algorithm works in two phases ?
- Phase 1: Process each growing day (0 to n-1)
- Phase 2: Continue eating remaining apples after day n
Step-by-Step Process
- Initialize a min-heap to store (expiration_day, apple_count) pairs
- For each day i from 0 to n-1:
- Remove expired apples from heap
- Add new apples grown on day i to heap
- Eat one apple from the batch expiring earliest
- After day n, continue eating remaining apples until heap is empty
Implementation
import heapq
def solve(apples, days):
minheap = []
day = 0
res = 0
# Phase 1: Process each growing day
for i in range(len(apples)):
day = i
# Remove expired apples
while minheap and minheap[0][0] < day:
heapq.heappop(minheap)
# Add new apples if any grown today
if apples[i] > 0:
expiration = i + days[i] - 1
heapq.heappush(minheap, (expiration, apples[i]))
# Eat one apple from earliest expiring batch
if minheap:
date, apple_count = heapq.heappop(minheap)
res += 1
# Put back remaining apples if any
if apple_count > 1:
heapq.heappush(minheap, (date, apple_count - 1))
# Phase 2: Continue eating after growing period
while minheap:
day += 1
# Remove expired apples
while minheap and minheap[0][0] < day:
heapq.heappop(minheap)
if not minheap:
break
# Eat one apple from earliest expiring batch
date, apple_count = heapq.heappop(minheap)
res += 1
# Put back remaining apples if any
if apple_count > 1:
heapq.heappush(minheap, (date, apple_count - 1))
return res
# Test the solution
apples = [1, 2, 3, 5, 2]
days = [3, 2, 1, 4, 2]
result = solve(apples, days)
print(f"Maximum apples eaten: {result}")
Maximum apples eaten: 7
How It Works
Let's trace through the example step by step ?
| Day | New Apples | Expires On | Heap State | Eaten | Total Eaten |
|---|---|---|---|---|---|
| 0 | 1 apple | Day 2 | [(2, 1)] | 1 | 1 |
| 1 | 2 apples | Day 2 | [(2, 2)] | 1 | 2 |
| 2 | 3 apples | Day 2 | [(2, 1), (2, 3)] | 1 | 3 |
| 3 | 5 apples | Day 6 | [(6, 5)] | 1 | 4 |
| 4 | 2 apples | Day 5 | [(5, 2), (6, 4)] | 1 | 5 |
| 5-6 | None | - | Continue eating | 2 | 7 |
Key Points
- Use min-heap to always eat apples expiring earliest
- Remove expired apples before processing each day
- Continue eating after the growing period ends
- Time complexity: O(n log n) due to heap operations
Conclusion
This greedy approach using a min-heap ensures we maximize apple consumption by prioritizing those that expire earliest. The algorithm efficiently handles both the growing period and the extended eating period.
