We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b99d324 commit 42b4851Copy full SHA for 42b4851
Hard/Constrained Subsequence Sum.java
@@ -0,0 +1,16 @@
1
+class Solution {
2
+ public int constrainedSubsetSum(int[] nums, int k) {
3
+ PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> b[0] - a[0]);
4
+ pq.add(new int[]{nums[0], 0});
5
+ int result = nums[0];
6
+ for (int i = 1; i < nums.length; i++) {
7
+ while (i - pq.peek()[1] > k) {
8
+ pq.remove();
9
+ }
10
+ int curr = Math.max(0, pq.peek()[0]) + nums[i];
11
+ result = Math.max(result, curr);
12
+ pq.add(new int[]{curr, i});
13
14
+ return result;
15
16
+}
0 commit comments