- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1354.java
30 lines (28 loc) · 916 Bytes
/
_1354.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
packagecom.fishercoder.solutions.secondthousand;
importjava.util.PriorityQueue;
publicclass_1354 {
publicstaticclassSolution1 {
/*
* Use % is the key here to avoid TLE, a good test case for this is [1,1000000000]
*/
publicbooleanisPossible(int[] target) {
longsum = 0;
PriorityQueue<Integer> maxHeap = newPriorityQueue<>((a, b) -> b - a);
for (inti = 0; i < target.length; i++) {
maxHeap.offer(target[i]);
sum += target[i];
}
while (maxHeap.peek() != 1) {
intmax = maxHeap.poll();
sum -= max;
if (max <= sum || sum < 1) {
returnfalse;
}
max %= sum;
sum += max;
maxHeap.offer(max);
}
returntrue;
}
}
}