Leetcode 1824 Solution
This article provides solution to leetcode question 1824 (maximum-number-of-eaten-apples)
Access this page by simply typing in "lcs 1824" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/maximum-number-of-eaten-apples
Solution
class Solution:
def eatenApples(self, apples: List[int], days: List[int]) -> int:
q = []
data = list(zip(apples, days))
t = 0
ans = 0
while q or t < len(data):
# step 1: expire apples
while q and q[0][0] <= t:
heapq.heappop(q)
# step 2: add new apples
if t < len(data):
apple, day = data[t]
if apple > 0 and day > 0:
heapq.heappush(q, (t + day, apple))
# step 3: eat apple if possible
if q:
expire_day, apple = heapq.heappop(q)
ans += 1
apple -= 1
if apple > 0:
heapq.heappush(q, (expire_day, apple))
t += 1
return ans