g1701_1800.s1705_maximum_number_of_eaten_apples.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g1701_1800.s1705_maximum_number_of_eaten_apples;
// #Medium #Array #Greedy #Heap_Priority_Queue
// #2022_04_24_Time_53_ms_(77.63%)_Space_45.2_MB_(80.26%)
import java.util.PriorityQueue;
/**
* 1705 - Maximum Number of Eaten Apples\.
*
* Medium
*
* 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._
*
* **Example 1:**
*
* **Input:** apples = [1,2,3,5,2], days = [3,2,1,4,2]
*
* **Output:** 7
*
* **Explanation:** 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.
*
* **Example 2:**
*
* **Input:** apples = [3,0,0,0,0,2], days = [3,0,0,0,0,2]
*
* **Output:** 5
*
* **Explanation:** 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.
*
* **Constraints:**
*
* * `n == apples.length == days.length`
* * 1 <= n <= 2 * 104
* * 0 <= apples[i], days[i] <= 2 * 104
* * `days[i] = 0` if and only if `apples[i] = 0`.
**/
public class Solution {
public int eatenApples(int[] apples, int[] days) {
PriorityQueue minHeap = new PriorityQueue<>((a, b) -> a[0] - b[0]);
int eatenApples = 0;
for (int i = 0; i < apples.length || !minHeap.isEmpty(); i++) {
if (i < apples.length) {
minHeap.offer(new int[] {i + days[i], apples[i]});
}
while (!minHeap.isEmpty() && (minHeap.peek()[0] <= i || minHeap.peek()[1] <= 0)) {
minHeap.poll();
}
if (!minHeap.isEmpty()) {
eatenApples++;
minHeap.peek()[1]--;
}
}
return eatenApples;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy