Jump Game

bong bong·2023년 8월 25일

알고리즘

목록 보기
23/31

풀이

canReach 배열에 각 nums 요소들을 도달할수 있는지 를 저장한다.
끝지점에 도달할수 있는지를 반환하면된다.!

문제

정수 배열이 제공됩니다 nums. 처음에는 배열의 첫 번째 인덱스 에 위치하며 배열의 각 요소는 해당 위치에서의 최대 점프 길이를 나타냅니다.
true마지막 인덱스에 도달할 수 있으면 반환하고 false그렇지 않으면 를 반환합니다 .

class Solution {
    public boolean canJump(int[] nums) {
     int length = nums.length;
        boolean[] canReach = new boolean[length];
        canReach[0] = true;
         for (int i = 1; i < length; i++) {
            for (int j = 0; j < i; j++) {
                if (canReach[j] && j + nums[j] >= i) {
                    canReach[i] = true;
                    break;
                }
            }
        }
        return canReach[length - 1];
    }
}
profile
let's go invent tomorrow rather than worrying about what happened yesterday - Steven Paul Jobs

0개의 댓글