Simple Heap Based approach
55
from heapq import heappush as hpush
from heapq import heappop as hpop

class Solution:
    def eatenApples(self, apples: List[int], days: List[int]) -> int:
        
        current_apple_to_eat = []
        current_day = 0
        max_apples = 0
        
        while current_day < len(apples) or len(current_apple_to_eat):
                
            # add apples that are available
            if current_day < len(apples) and apples[current_day]:
                hpush(current_apple_to_eat, (current_day + days[current_day], apples[current_day]))                
                
            # if no apples left to eat, try again another day
            if not len(current_apple_to_eat):
                current_day += 1
                continue
                
            # pick an apple to eat
            curr_day, num_apples = hpop(current_apple_to_eat)
            max_apples += 1
            
            # add back to the heap if still apples left
            num_apples -= 1
            if num_apples:
                hpush(current_apple_to_eat, (curr_day, num_apples))
                
            # make sure all apples that will rot the next day are removed
            while len(current_apple_to_eat) and current_day + 1 == current_apple_to_eat[0][0]:
                hpop(current_apple_to_eat)
                
            current_day += 1

        return max_apples
Comments (1)