Maximum Number of Eaten Apples LeetCode solution in Python

1
import heapq
2

3

4
class Solution(object):
5
def eatenApples(self, apples, days):
6
"""
7
:type apples: List[int]
8
:type days: List[int]
9
:rtype: int
10
"""
11
heap = [(days[0], apples[0])]
12
heapq.heapify(heap)
13
day = 0
14
rtn = 0
15
while heap or day < len(days):
16
# print(heap, day)
17
apple = 0
18
if heap:
19
cnt, apple = heapq.heappop(heap)
20
while heap and cnt <= day and apple > 0:
21
cnt, apple = heapq.heappop(heap)
22
if apple > 0 and cnt > day:
23
rtn += 1
24
day += 1
25
if apple > 1 and cnt > day:
26
heapq.heappush(heap, (cnt, apple - 1))
27
if day < len(days) and apples[day] > 0:
28
heapq.heappush(heap, (day + days[day], apples[day]))
29
return rtn

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0