forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2335.java
38 lines (35 loc) · 1.03 KB
/
_2335.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
27
28
29
30
31
32
33
34
35
36
37
38
package com.fishercoder.solutions;
import java.util.PriorityQueue;
public class _2335 {
public static class Solution1 {
public int fillCups(int[] amount) {
PriorityQueue<Integer> heap = new PriorityQueue<>((a, b) -> b - a);
for (int num : amount) {
if (num > 0) {
heap.offer(num);
}
}
int seconds = 0;
while (!heap.isEmpty()) {
if (heap.size() == 1) {
seconds += heap.poll();
return seconds;
}
int one = heap.poll();
one--;
if (!heap.isEmpty()) {
int two = heap.poll();
two--;
if (two > 0) {
heap.offer(two);
}
}
if (one > 0) {
heap.offer(one);
}
seconds++;
}
return seconds;
}
}
}