1705. Maximum Number of Eaten Apples

Обновлено: 2024-03-12
1 мин
[]

Содержание

LeetCode problem 1705

class Solution:
    def eatenApples(self, apples: List[int], days: List[int]) -> int:
        n = len(days)
        i = res = 0
        q = []
        while i < n or q:
            if i < n and apples[i]:
                heappush(q, (i + days[i] - 1, apples[i]))
            while q and q[0][0] < i:
                heappop(q)
            if q:
                t, v = heappop(q)
                v -= 1
                res += 1
                if v and t > i:
                    heappush(q, (t, v))
            i += 1
        return res