Skip to content

Commit ff815fc

Browse files
add 2389
1 parent 43eb6c1 commit ff815fc

File tree

3 files changed

+65
-0
lines changed
  • paginated_contents/algorithms/3rd_thousand
  • src

3 files changed

+65
-0
lines changed

Diff for: paginated_contents/algorithms/3rd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
| 2404 | [Most Frequent Even Element](https://leetcode.com/problems/most-frequent-even-element/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2404.java) || Easy ||
5959
| 2399 | [Check Distances Between Same Letters](https://leetcode.com/problems/check-distances-between-same-letters/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2399.java) || Medium ||
6060
| 2395 | [Find Subarrays With Equal Sum](https://leetcode.com/problems/find-subarrays-with-equal-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2395.java) || Easy ||
61+
| 2389 | [Longest Subsequence With Limited Sum](https://leetcode.com/problems/longest-subsequence-with-limited-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2389.java) || Easy |
6162
| 2385 | [Amount of Time for Binary Tree to Be Infected](https://leetcode.com/problems/amount-of-time-for-binary-tree-to-be-infected/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2380.java) || Medium | BFS
6263
| 2380 | [Time Needed to Rearrange a Binary String](https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2380.java) || Medium ||
6364
| 2379 | [Minimum Recolors to Get K Consecutive Black Blocks](https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2379.java) || Easy ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fishercoder.solutions.thirdthousand;
2+
3+
import java.util.Map;
4+
import java.util.TreeMap;
5+
6+
public class _2389 {
7+
public static class Solution1 {
8+
/**
9+
* My completely original solution, not sure why it's labeled EASY, IMHO, it should be a soft MEDIUM.
10+
*/
11+
public int[] answerQueries(int[] nums, int[] queries) {
12+
TreeMap<Integer, Integer> map = new TreeMap<>();
13+
int total = 0;
14+
for (int num : nums) {
15+
map.put(num, map.getOrDefault(num, 0) + 1);
16+
total += num;
17+
}
18+
int[] answer = new int[queries.length];
19+
for (int i = 0; i < queries.length; i++) {
20+
int sum = total;
21+
int len = nums.length;
22+
TreeMap<Integer, Integer> copy = new TreeMap<>(map);
23+
if (sum <= queries[i]) {
24+
answer[i] = len;
25+
} else {
26+
do {
27+
sum -= copy.lastKey();
28+
len--;
29+
if (sum <= queries[i]) {
30+
answer[i] = len;
31+
break;
32+
}
33+
Map.Entry<Integer, Integer> lastEntry = copy.pollLastEntry();
34+
if (lastEntry.getValue() > 1) {
35+
copy.put(lastEntry.getKey(), lastEntry.getValue() - 1);
36+
}
37+
} while (sum > queries[i]);
38+
}
39+
}
40+
return answer;
41+
}
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fishercoder.thirdthousand;
2+
3+
import com.fishercoder.solutions.thirdthousand._2389;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
8+
9+
public class _2389Test {
10+
private static _2389.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _2389.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertArrayEquals(new int[]{2, 3, 4}, solution1.answerQueries(new int[]{4, 5, 2, 1}, new int[]{3, 10, 21}));
20+
}
21+
}

0 commit comments

Comments
 (0)