forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1705.java
26 lines (24 loc) · 969 Bytes
/
_1705.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.fishercoder.solutions;
import java.util.PriorityQueue;
public class _1705 {
public static class Solution1 {
public int eatenApples(int[] apples, int[] days) {
/**we sort the heap by its expiration dates, we'll eat the earliest expiration apples first*/
PriorityQueue<int[]> 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;
}
}
}