forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2126.java
56 lines (53 loc) · 1.85 KB
/
_2126.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.fishercoder.solutions;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class _2126 {
public static class Solution1 {
public boolean asteroidsDestroyed(int mass, int[] asteroids) {
Map<Integer, Integer> map = new HashMap<>();
for (int a : asteroids) {
map.put(a, map.getOrDefault(a, 0) + 1);
}
int[] nums = new int[map.keySet().size()];
int i = 0;
for (int key : map.keySet()) {
nums[i++] = key;
}
Arrays.sort(nums);
int startIndex = 0;
long sum = mass;
int upToIndex = binarySearch(sum, nums, startIndex, nums.length - 1);
while (upToIndex < nums.length) {
for (i = startIndex; i <= upToIndex; i++) {
sum += (long) map.get(nums[i]) * nums[i];
}
if (upToIndex == nums.length - 1) {
return true;
}
startIndex = upToIndex + 1;
upToIndex = binarySearch(sum, nums, startIndex, nums.length - 1);
if (startIndex > upToIndex) {
return false;
}
}
return true;
}
private int binarySearch(long sum, int[] nums, int left, int right) {
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] < sum) {
left = mid + 1;
} else if (nums[mid] > sum) {
right = mid - 1;
} else {
return mid;
}
}
return right < nums.length && nums[right] <= sum ? right : left - 1;
}
}
}