Destroying Asteroids LeetCode solution in Java

1
class Solution {
2
public boolean asteroidsDestroyed(int mass, int[] asteroids) {
3
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
4
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
5

6
for (int val : asteroids) maxHeap.add(val);
7

8
long bigMass = mass;
9

10
while (maxHeap.size() > 0) {
11
int curr = maxHeap.poll();
12

13
if (bigMass >= curr) {
14
bigMass += curr;
15
while (minHeap.size() > 0 && bigMass >= minHeap.peek()) {
16
bigMass += minHeap.poll();
17
}
18
} else {
19
minHeap.add(curr);
20
}
21
}
22

23
return minHeap.size() == 0 && maxHeap.size() == 0;
24
}
25
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0