-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjump-game.js
50 lines (46 loc) · 1.26 KB
/
jump-game.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Source: https://leetcode.com/problems/jump-game/
* Tags: [Array,Greedy]
* Level: Medium
* Title: Jump Game
* Auther: @imcoddy
* Content: Given an array of non-negative integers, you are initially positioned at the first index of the array.
*
*
* Each element in the array represents your maximum jump length at that position.
*
*
* Determine if you are able to reach the last index.
*
*
*
* For example:
* A = [2,3,1,1,4], return true.
*
*
* A = [3,2,1,0,4], return false.
*/
/**
* @param {number[]} nums
* @return {boolean}
*/
/**
* Memo: If we can jump to last position, it means at some point we can reach from last-i if nums[last-i] >= i, as there are i steps from that to last. So we can make this to a smaller problem, until we confirm whether we can reach the 1st location.
* Complex: O(n)
* Runtime: 132ms
* Tests: 71 test cases passed
* Rank: A
* Updated: 2015-06-04
*/
var canJump = function(nums) {
var last = nums.length - 1;
for (var i = nums.length - 2; i >= 0; i--) {
if (i + nums[i] >= last) last = i;
}
return last === 0;
};
var should = require('should');
console.time('Runtime');
canJump([2, 3, 1, 1, 4]).should.equal(true);
canJump([3, 2, 1, 0, 4]).should.equal(false);
console.timeEnd('Runtime');