Maximum number of apples that can be eaten by a person | C # program of the above approach ; 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
using System ; using System . Collections . Generic ; class GFG { static int cntMaxApples ( int [ ] apples , int [ ] days ) { List < Tuple < int , int > > pq = new List < Tuple < int , int > > ( ) ; int i = 0 ; int n = apples . Length ; int total_apples = 0 ; while ( i < n . Count > 0 ) { if ( i < n && apples [ i ] != 0 ) { pq . Add ( new Tuple < int , int > ( i + days [ i ] - 1 , apples [ i ] ) ) ; pq . Sort ( ) ; } while ( pq . Count > 0 && pq [ 0 ] . Item1 < i ) { pq . RemoveAt ( 0 ) ; } if ( pq . Count > 0 ) { Tuple < int , int > curr = pq [ 0 ] ; pq . RemoveAt ( 0 ) ; if ( curr . Item2 > 1 ) { pq . Add ( new Tuple < int , int > ( curr . Item1 , curr . Item2 - 1 ) ) ; pq . Sort ( ) ; } ++ total_apples ; } ++ i ; } return total_apples ; } static void Main ( ) { int [ ] apples = { 1 , 2 , 3 , 5 , 2 } ; int [ ] days = { 3 , 2 , 1 , 4 , 2 } ; Console . Write ( cntMaxApples ( apples , days ) ) ; } }