File tree
Expand file treeCollapse file tree1 file changed
+16
-0
lines changed Expand file treeCollapse file tree1 file changed
+16
-0
lines changed Original file line number | Diff line number | Diff line change |
---|
|
| 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 | +} |
You can’t perform that action at this time.
0 commit comments