Skip to content

Commit

Permalink
Create JumpGameVI.java
Browse files Browse the repository at this point in the history
  • Loading branch information
jsjtzyy authored Apr 18, 2021
1 parent 1c5b023 commit 7ebb577
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions MonotoneQueue/JumpGameVI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
You are given a 0-indexed integer array nums and an integer k.
You are initially standing at index 0. In one move, you can jump at most k steps forward without going outside the boundaries of the array. That is, you can jump from index i to any index in the range [i + 1, min(n - 1, i + k)] inclusive.
You want to reach the last index of the array (index n - 1). Your score is the sum of all nums[j] for each index j you visited in the array.
Return the maximum score you can get.
problem link: https://leetcode.com/problems/jump-game-vi/
*/
class JumpGameVI {
public int maxResult(int[] nums, int k) {
Deque<Integer> queue = new LinkedList<>();
queue.addLast(nums[0]);
int cur = 0;
int[] res = new int[nums.length];
res[0] = nums[0];
for(int i = 1; i < nums.length; ++i){
cur = queue.peekFirst() + nums[i];
res[i] = cur;
while(!queue.isEmpty() && cur > queue.peekLast()){
queue.pollLast();
}
queue.addLast(cur);
if(i - k >= 0){
if(queue.peekFirst() == res[i - k]){
queue.pollFirst();
}
}
}
return res[nums.length - 1];
}
}

0 comments on commit 7ebb577

Please sign in to comment.