Skip to content

Commit 45187e1

Browse files
solves #2389: Longest Subsequence With Limited Sum in java
1 parent acf2c2b commit 45187e1

File tree

3 files changed

+55
-31
lines changed

3 files changed

+55
-31
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@
762762
| 2373 | [Largest Local Values in a Matrix](https://leetcode.com/problems/largest-local-values-in-a-matrix) | [![Java](assets/java.png)](src/LargestLocalValuesInAMatrix.java) | |
763763
| 2379 | [Minimum Recolors to Get K Consecutive Black Blocks](https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks) | [![Java](assets/java.png)](src/MinimumRecolorsToGetKConsecutiveBlackBlocks.java) | |
764764
| 2383 | [Minimum Hours of Training to Win a Competition](https://leetcode.com/problems/minimum-hours-of-training-to-win-a-competition) | [![Java](assets/java.png)](src/MinimumHoursOfTrainingToWinACompetition.java) | |
765-
| 2389 | [Longest Subsequence With Limited Sum](https://leetcode.com/problems/longest-subsequence-with-limited-sum) | | |
765+
| 2389 | [Longest Subsequence With Limited Sum](https://leetcode.com/problems/longest-subsequence-with-limited-sum) | [![Java](assets/java.png)](src/LongestSubsequenceWithLimitedSum.java) | |
766766
| 2395 | [Find Subarrays With Equal Sum](https://leetcode.com/problems/find-subarrays-with-equal-sum) | | |
767767
| 2399 | [Check Distances Between Same Letters](https://leetcode.com/problems/check-distances-between-same-letters) | | |
768768
| 2404 | [Most Frequent Even Element](https://leetcode.com/problems/most-frequent-even-element) | | |

src/HelloWorld.java

+8-30
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,13 @@
1+
import java.util.PriorityQueue;
2+
import java.util.Queue;
3+
14
public class HelloWorld {
25
public static void main(String[] args) {
3-
System.out.println(
4-
LargestNumber.largestNumber(new int[] {1, 2, 3})
5-
);
6-
7-
System.out.println(
8-
LargestNumber.largestNumber(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9})
9-
);
10-
11-
System.out.println(
12-
LargestNumber.largestNumber(new int[] {10, 2})
13-
);
14-
15-
System.out.println(
16-
LargestNumber.largestNumber(new int[] {1})
17-
);
18-
19-
System.out.println(
20-
LargestNumber.largestNumber(new int[] {1, 10, 11, 12})
21-
);
22-
23-
System.out.println(
24-
LargestNumber.largestNumber(new int[] {1, 10, 11, 12, 16, 15, 14, 13, 19, 18, 17})
25-
);
26-
27-
System.out.println(
28-
LargestNumber.largestNumber(new int[] {3, 30, 34, 5, 9})
29-
);
6+
final Queue<Integer> queue = new PriorityQueue<>();
7+
queue.add(10);
8+
queue.add(4);
9+
queue.add(-2);
3010

31-
System.out.println(
32-
LargestNumber.largestNumber(new int[] {432, 43243})
33-
);
11+
System.out.println(queue.peek());
3412
}
3513
}
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// https://leetcode.com/problems/longest-subsequence-with-limited-sum
2+
// T: O((n + m) log(n))
3+
// S: O(n)
4+
5+
import java.util.PriorityQueue;
6+
import java.util.Queue;
7+
8+
public class LongestSubsequenceWithLimitedSum {
9+
public int[] answerQueries(int[] nums, int[] queries) {
10+
final int[] result = new int[queries.length];
11+
final int[] prefixSumArray = getMinSumArray(nums);
12+
13+
for (int index = 0 ; index < queries.length ; index++) {
14+
int maxLen = binarySearch(prefixSumArray, queries[index]);
15+
if (maxLen < prefixSumArray.length && prefixSumArray[maxLen] == queries[index]) {
16+
result[index] = maxLen + 1;
17+
} else result[index] = maxLen;
18+
}
19+
20+
return result;
21+
}
22+
23+
private int binarySearch(int[] array, int x) {
24+
int left = 0, right = array.length - 1, index = 0;
25+
while (left <= right) {
26+
index = left + (right - left) / 2;
27+
if (array[index] == x) return index;
28+
else if (array[index] > x) right = index - 1;
29+
else left = index + 1;
30+
}
31+
return left;
32+
}
33+
34+
private int[] getMinSumArray(int[] array) {
35+
final Queue<Integer> minHeap = new PriorityQueue<>();
36+
final int[] result = new int[array.length];
37+
for (int element : array) {
38+
minHeap.add(element);
39+
}
40+
result[0] = minHeap.poll();
41+
for (int index = 1 ; index < result.length ; index++) {
42+
result[index] = result[index - 1] + minHeap.poll();
43+
}
44+
return result;
45+
}
46+
}

0 commit comments

Comments
 (0)