forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1354.java
30 lines (28 loc) · 901 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
package com.fishercoder.solutions;
import java.util.PriorityQueue;
public class _1354 {
public static class Solution1 {
/**
* Use % is the key here to avoid TLE, a good test case for this is [1,1000000000]
*/
public boolean isPossible(int[] target) {
long sum = 0;
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
for (int i = 0; i < target.length; i++) {
maxHeap.offer(target[i]);
sum += target[i];
}
while (maxHeap.peek() != 1) {
int max = maxHeap.poll();
sum -= max;
if (max <= sum || sum < 1) {
return false;
}
max %= sum;
sum += max;
maxHeap.offer(max);
}
return true;
}
}
}