문제
You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.
Return true if you can reach the last index, or false otherwise.
입출력
Example 1:
Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
Input: nums = [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
제약
Constraints:
- 1 <= nums.length <= 10^4
- 0 <= nums[i] <= 10^5
코드
var canJump = function(nums) {
if(nums.length==1) return true;
var Dest = nums.length-1;
for(var i=nums.length-2; i>=0; i--) {
if(nums[i]+i>=Dest) Dest= i;
}
if(Dest==0) return true;
return false;
};
'Coding > LeetCode' 카테고리의 다른 글
[LeetCode/JavaScript] 29. Divide Two Integers (medium) (0) | 2021.11.04 |
---|---|
[LeetCode/JavaScript] 58. Length of Last Word (easy) (0) | 2021.11.03 |
[LeetCode/JavaScript] 11. Container With Most Water (medium) (0) | 2021.10.07 |
[LeetCode/JavaScript] 695. Max Area of Island (medium) (0) | 2021.09.23 |
[LeetCode/JavaScript] 733. Flood Fill (easy) (0) | 2021.09.21 |