https://leetcode.com/problems/jump-game/
Jump Game - LeetCode
Can you solve this real interview question? Jump Game - 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
leetcode.com
0번 인덱스부터 최종 인덱스까지 각 인덱스에서의 nums[i] 수치만큼 이동할 수 있을때 끝까지 도달할 수 있는지에 대한 문제이다.
최종 목표지에 도달하기 위해선 특정 인덱스에서의 점프력(값이) 마지막 인덱스보다 높아야 한다.
그리고 해당 인덱스 까지 도달하기 위해서는 그 이전에서 점프력이 넘어야한다.
이를 반복해서 최초 시작 지점에서 목표 지점까지 뛸 수 있다면 가능하다고 할 수 있다.
배열을 역으로 내려오면 확인하며 풀었다.
public class Solution {
public boolean canJump(int[] nums) {
int goal = nums.length - 1;
for(int i = nums.length - 1; i >= 0; i--){
int num = nums[i];
if(i + num >= goal){
goal = i;
}
}
return goal == 0;
}
}
