Skip to content

Commit df95f97

Browse files
committed
Add solution #1696
1 parent e9a9ab8 commit df95f97

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,482 LeetCode solutions in JavaScript
1+
# 1,483 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1307,6 +1307,7 @@
13071307
1691|[Maximum Height by Stacking Cuboids](./solutions/1691-maximum-height-by-stacking-cuboids.js)|Hard|
13081308
1694|[Reformat Phone Number](./solutions/1694-reformat-phone-number.js)|Easy|
13091309
1695|[Maximum Erasure Value](./solutions/1695-maximum-erasure-value.js)|Medium|
1310+
1696|[Jump Game VI](./solutions/1696-jump-game-vi.js)|Medium|
13101311
1716|[Calculate Money in Leetcode Bank](./solutions/1716-calculate-money-in-leetcode-bank.js)|Easy|
13111312
1718|[Construct the Lexicographically Largest Valid Sequence](./solutions/1718-construct-the-lexicographically-largest-valid-sequence.js)|Medium|
13121313
1726|[Tuple with Same Product](./solutions/1726-tuple-with-same-product.js)|Medium|

solutions/1696-jump-game-vi.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* 1696. Jump Game VI
3+
* https://leetcode.com/problems/jump-game-vi/
4+
* Difficulty: Medium
5+
*
6+
* You are given a 0-indexed integer array nums and an integer k.
7+
*
8+
* You are initially standing at index 0. In one move, you can jump at most k steps forward without
9+
* going outside the boundaries of the array. That is, you can jump from index i to any index in
10+
* the range [i + 1, min(n - 1, i + k)] inclusive.
11+
*
12+
* You want to reach the last index of the array (index n - 1). Your score is the sum of all
13+
* nums[j] for each index j you visited in the array.
14+
*
15+
* Return the maximum score you can get.
16+
*/
17+
18+
/**
19+
* @param {number[]} nums
20+
* @param {number} k
21+
* @return {number}
22+
*/
23+
var maxResult = function(nums, k) {
24+
const n = nums.length;
25+
const maxScores = new Array(n).fill(-Infinity);
26+
const deque = [];
27+
28+
maxScores[0] = nums[0];
29+
deque.push(0);
30+
31+
for (let i = 1; i < n; i++) {
32+
while (deque.length && deque[0] < i - k) {
33+
deque.shift();
34+
}
35+
36+
maxScores[i] = nums[i] + maxScores[deque[0]];
37+
38+
while (deque.length && maxScores[i] >= maxScores[deque[deque.length - 1]]) {
39+
deque.pop();
40+
}
41+
42+
deque.push(i);
43+
}
44+
45+
return maxScores[n - 1];
46+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy