There is a special kind of apple tree that grows apples every day for n
days. On the ith day, the tree grows apples[i] apples that will rot after
days[i] days, that is on day i + days[i] the apples will be rotten and cannot be eaten. On some days, the apple tree does not grow any apples, which are denoted by apples[i] == 0 and days[i] == 0.
You decided to eat at most one apple a day (to keep the doctors away).
Note that you can keep eating after the first n days.
Given two integer arrays days and apples of length n, return the maximum number of apples you can eat.
Input: apples =[1,2,3,5,2], days =[3,2,1,4,2]Output: 7Explanation: You can eat 7 apples:- On the first day, you eat an apple that grew on the first day.- On the second day, you eat an apple that grew on the second day.- On the third day, you eat an apple that grew on the second day. After this day, the apples that grew on the third day rot.- On the fourth to the seventh days, you eat apples that grew on the fourth day.
Input: apples =[3,0,0,0,0,2], days =[3,0,0,0,0,2]Output: 5Explanation: You can eat 5 apples:- On the first to the third day you eat apples that grew on the first day.- Do nothing on the fouth and fifth days.- On the sixth and seventh days you eat apples that grew on the sixth day.
To maximize the number of apples eaten, always eat the apple that will rot the soonest. Use a min-heap to keep track of apple batches by their expiration day, and eat one apple per day from the batch that expires first.
classSolution {
public:int eatenApples(vector<int>& apples, vector<int>& days) {
int n = apples.size(), ans =0, i =0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
while (i < n ||!pq.empty()) {
if (i < n && apples[i] >0) {
pq.emplace(i + days[i], apples[i]);
}
while (!pq.empty() && (pq.top().first <= i || pq.top().second ==0)) pq.pop();
if (!pq.empty()) {
auto p = pq.top(); pq.pop();
ans++;
if (--p.second >0) pq.push(p);
}
i++;
}
return ans;
}
};
import java.util.*;
classSolution {
publicinteatenApples(int[] apples, int[] days) {
int n = apples.length, ans = 0, i = 0;
PriorityQueue<int[]> pq =new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
while (i < n ||!pq.isEmpty()) {
if (i < n && apples[i]> 0) {
pq.offer(newint[]{i + days[i], apples[i]});
}
while (!pq.isEmpty() && (pq.peek()[0]<= i || pq.peek()[1]== 0)) pq.poll();
if (!pq.isEmpty()) {
int[] p = pq.poll();
ans++;
if (--p[1]> 0) pq.offer(p);
}
i++;
}
return ans;
}
}
import java.util.PriorityQueue
classSolution {
funeatenApples(apples: IntArray, days: IntArray): Int {
val n = apples.size
var ans = 0var i = 0val pq = PriorityQueue(compareBy<Pair<Int, Int>> { it.first })
while (i < n || pq.isNotEmpty()) {
if (i < n && apples[i] > 0) {
pq.add(Pair(i + days[i], apples[i]))
}
while (pq.isNotEmpty() && (pq.peek().first <= i || pq.peek().second ==0)) pq.poll()
if (pq.isNotEmpty()) {
val p = pq.poll()
ans++if (p.second - 1 > 0) pq.add(Pair(p.first, p.second - 1))
}
i++ }
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import heapq
classSolution:
defeatenApples(self, apples: list[int], days: list[int]) -> int:
n, ans, i = len(apples), 0, 0 heap = []
while i < n or heap:
if i < n and apples[i] >0:
heapq.heappush(heap, (i + days[i], apples[i]))
while heap and (heap[0][0] <= i or heap[0][1] ==0):
heapq.heappop(heap)
if heap:
expire, count = heapq.heappop(heap)
ans +=1if count -1>0:
heapq.heappush(heap, (expire, count -1))
i +=1return ans
use std::collections::BinaryHeap;
use std::cmp::Reverse;
impl Solution {
pubfneaten_apples(apples: Vec<i32>, days: Vec<i32>) -> i32 {
use std::collections::BinaryHeap;
let n = apples.len();
letmut ans =0;
letmut i =0;
letmut heap = BinaryHeap::new();
while i < n ||!heap.is_empty() {
if i < n && apples[i] >0 {
heap.push(Reverse((i asi32+ days[i], apples[i])));
}
whilelet Some(&Reverse((expire, count))) = heap.peek() {
if expire <= i asi32|| count ==0 {
heap.pop();
} else {
break;
}
}
iflet Some(Reverse((expire, mut count))) = heap.pop() {
ans +=1;
count -=1;
if count >0 {
heap.push(Reverse((expire, count)));
}
}
i +=1;
}
ans
}
}