Maximum number of apples that can be eaten by a person
Given two arrays apples[] and days[] representing the count of apples an apple tree produces and the number of days these apples are edible from the ith day respectively, the task is to find the maximum number of apples a person can eat if the person can eat at most one apple in a day.
Examples
Input: apples[] = { 1, 2, 3, 5, 2 }, days[] = { 3, 2, 1, 4, 2 }
Output: 7
Explanation:
On 1st day, person eats the apple produced by apple tree on the 1st day.
On 2nd day, person eats the apple produced by apple tree on the 2nd day.
On 3rd day, person eats the apple produced by apple tree on the 2nd day.
On 4th to 7th day, person eats the apple produced by apple tree on the 4th day.
Input: apples[] = { 3, 0, 0, 0, 0, 2 }, days[] = { 3, 0, 0, 0, 0, 2 }
Output: 5
Approach: The idea is to eat the apples having the closest expiration date. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int cntMaxApples(vector< int > apples, vector< int > days)
{
typedef pair< int , int > P;
priority_queue<P, vector <P>, greater <P> > pq;
int i = 0;
int n = apples.size();
int total_apples = 0;
while (i < n || !pq.empty()) {
if (i < n && apples[i] != 0) {
pq.push({ i + days[i] - 1, apples[i] });
}
while (!pq.empty() && pq.top().first < i) {
pq.pop();
}
if (!pq.empty()) {
auto curr = pq.top();
pq.pop();
if (curr.second > 1) {
pq.push({ curr.first,
curr.second - 1 });
}
++total_apples;
}
++i;
}
return total_apples;
}
int main()
{
vector< int > apples = { 1, 2, 3, 5, 2 };
vector< int > days = { 3, 2, 1, 4, 2 };
cout << cntMaxApples(apples, days);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int cntMaxApples( int [] apples, int [] days)
{
class Pair {
int first, second;
Pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
PriorityQueue<Pair> pq = new PriorityQueue<>((a, b) -> a.first - b.first);
int i = 0 ;
int n = apples.length;
int total_apples = 0 ;
while (i < n || !pq.isEmpty())
{
if (i < n && apples[i] != 0 )
{
pq.add( new Pair(i + days[i] - 1 , apples[i]));
}
while (!pq.isEmpty() && pq.peek().first < i) {
pq.remove();
}
if (!pq.isEmpty())
{
Pair curr = pq.peek();
pq.remove();
if (curr.second > 1 )
{
pq.add( new Pair(curr.first,
curr.second - 1 ));
}
++total_apples;
}
++i;
}
return total_apples;
}
public static void main(String[] args)
{
int [] apples = { 1 , 2 , 3 , 5 , 2 };
int [] days = { 3 , 2 , 1 , 4 , 2 };
System.out.println(cntMaxApples(apples, days));
}
}
|
Python3
def cntMaxApples(apples, days) :
pq = []
i = 0
n = len (apples)
total_apples = 0
while (i < n or len (pq) > 0 ) :
if (i < n and apples[i] ! = 0 ) :
pq.append([i + days[i] - 1 , apples[i]])
pq.sort()
while ( len (pq) > 0 and pq[ 0 ][ 0 ] < i) :
pq.pop( 0 )
if ( len (pq) > 0 ) :
curr = pq[ 0 ]
pq.pop( 0 )
if ( len (curr) > 1 ) :
pq.append([curr[ 0 ], curr[ 1 ] - 1 ])
pq.sort()
total_apples + = 1
i + = 1
return total_apples
apples = [ 1 , 2 , 3 , 5 , 2 ]
days = [ 3 , 2 , 1 , 4 , 2 ]
print (cntMaxApples(apples, days))
|
C#
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 || pq.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));
}
}
|
Javascript
<script>
function cntMaxApples(apples, days) {
let pq = []
let i = 0
let n = apples.length;
let total_apples = 0
while (i < n || pq.length > 0) {
if (i < n && apples[i] != 0) {
pq.push([i + days[i] - 1, apples[i]])
pq.sort()
}
while (pq.length > 0 && pq[0][0] < i) {
pq.shift()
}
if (pq.length > 0) {
curr = pq[0]
pq.shift()
if (curr.length > 1) {
pq.push([curr[0], curr[1] - 1])
pq.sort()
}
total_apples += 1
}
i += 1
}
return total_apples
}
let apples = [1, 2, 3, 5, 2]
let days = [3, 2, 1, 4, 2]
document.write(cntMaxApples(apples, days))
</script>
|
Time Complexity: O(N log N)
Auxiliary Space: O(N)
Last Updated :
21 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...