-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_2591.java
39 lines (37 loc) · 1.19 KB
/
_2591.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
39
package com.fishercoder.solutions.thirdthousand;
import java.util.PriorityQueue;
public class _2591 {
public static class Solution1 {
public int distMoney(int money, int children) {
if (money / children == 8 && money % children == 0) {
return children;
}
if (money < children) {
return -1;
}
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
for (int i = 0; i < children; i++) {
minHeap.offer(1);
money--;
}
int maxEights = 0;
while (!minHeap.isEmpty() && money > 0) {
Integer curr = minHeap.poll();
if (money < 7) {
curr += money;
minHeap.offer(curr);
break;
} else if (minHeap.size() > 0) {
money -= 7;
maxEights++;
} else if (minHeap.size() == 0) {
break;
}
}
if (!minHeap.isEmpty() && minHeap.peek() == 4) {
maxEights--;
}
return maxEights;
}
}
}