-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_2389.java
43 lines (41 loc) · 1.53 KB
/
_2389.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
package com.fishercoder.solutions.thirdthousand;
import java.util.Map;
import java.util.TreeMap;
public class _2389 {
public static class Solution1 {
/*
* My completely original solution, not sure why it's labeled EASY, IMHO, it should be a soft MEDIUM.
*/
public int[] answerQueries(int[] nums, int[] queries) {
TreeMap<Integer, Integer> map = new TreeMap<>();
int total = 0;
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
total += num;
}
int[] answer = new int[queries.length];
for (int i = 0; i < queries.length; i++) {
int sum = total;
int len = nums.length;
TreeMap<Integer, Integer> copy = new TreeMap<>(map);
if (sum <= queries[i]) {
answer[i] = len;
} else {
do {
sum -= copy.lastKey();
len--;
if (sum <= queries[i]) {
answer[i] = len;
break;
}
Map.Entry<Integer, Integer> lastEntry = copy.pollLastEntry();
if (lastEntry.getValue() > 1) {
copy.put(lastEntry.getKey(), lastEntry.getValue() - 1);
}
} while (sum > queries[i]);
}
}
return answer;
}
}
}