Maximum number of apples that can be eaten by a person | Function to find the maximum number of apples a person can eat such that the person eat at most one apple in a day . ; Store count of apples and number of days those apples are edible ; Stores indices of the array ; Stores count of days ; Stores maximum count of edible apples ; Traverse both the arrays ; If top element of the apple is not already expired ; Insert count of apples and their expiration date ; Remove outdated apples ; Insert all the apples produces by tree on current day ; Stores top element of pq ; Remove top element of pq ; If count of apples in curr is greater than 0 ; Insert count of apples and their expiration date ; Update total_apples ; Update index ; Driver Code
def cntMaxApples ( apples , days ) : NEW_LINE INDENT pq = [ ] NEW_LINE i = 0 NEW_LINE n = len ( apples ) NEW_LINE total_apples = 0 NEW_LINE while ( i < n or len ( pq ) > 0 ) : NEW_LINE if ( i < n and apples [ i ] != 0 ) : NEW_LINE INDENT pq . append ( [ i + days [ i ] - 1 , apples [ i ] ] ) NEW_LINE pq . sort ( ) NEW_LINE DEDENT while ( len ( pq ) > 0 and pq [ 0 ] [ 0 ] < i ) : NEW_LINE INDENT pq . pop ( 0 ) NEW_LINE DEDENT if ( len ( pq ) > 0 ) : NEW_LINE INDENT curr = pq [ 0 ] NEW_LINE pq . pop ( 0 ) NEW_LINE if ( len ( curr ) > 1 ) : NEW_LINE pq . append ( [ curr [ 0 ] , curr [ 1 ] - 1 ] ) NEW_LINE pq . sort ( ) NEW_LINE total_apples += 1 NEW_LINE DEDENT i += 1 NEW_LINE return total_apples NEW_LINE DEDENT apples = [ 1 , 2 , 3 , 5 , 2 ] NEW_LINE days = [ 3 , 2 , 1 , 4 , 2 ] NEW_LINE print ( cntMaxApples ( apples , days ) ) NEW_LINE