Skip to content

Commit 20df14d

Browse files
committed
feat: add question 45
1 parent fc21467 commit 20df14d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

45.跳跃游戏-ii.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @lc app=leetcode.cn id=45 lang=javascript
3+
*
4+
* [45] 跳跃游戏 II
5+
*
6+
* 1. 贪心问题, 对于每轮跳跃操作, 找到当前能跳到最远的位置
7+
*/
8+
9+
// @lc code=start
10+
/**
11+
* @param {number[]} nums
12+
* @return {number}
13+
*/
14+
var jump = function(nums) {
15+
let end = 0;
16+
let max = 0;
17+
let step = 0;
18+
for (let i = 0; i < nums.length - 1; i++) {
19+
max = Math.max(max, i + nums[i]);
20+
if (i === end) {
21+
end = max;
22+
step++;
23+
}
24+
}
25+
return step;
26+
};
27+
// @lc code=end

0 commit comments

Comments
 (0)