about | help | code help+videos | done | prefs |
You are given an integer array nums where each element represents your maximum jump length at that position. You start at index 0, and your goal is to determine whether you can reach the last index of the array. From any position i, you may jump to any of the next nums[i] indices, that is, any index from i + 1 to i + nums[i]. You can only move forward, and you cannot jump beyond the array bounds. Return true if it is possible to reach the last index starting from index 0, otherwise return false. For example, if nums = {2,3,1,1,4}, you can jump from index 0 to 1, and then from index 1 to index 4, which is the last index, so the output is true. If nums = {3,2,1,0,4}, you will always get stuck at index 3 because its maximum jump length is 0, and you cannot reach index 4, so the output is false. You must solve this in linear time. canJump([2, 3, 1, 1, 4]) → true canJump([3, 2, 1, 0, 4]) → false canJump([0]) → true ...Save, Compile, Run (ctrl-enter) |
Progress graphs:
Your progress graph for this problem
Random user progress graph for this problem
Random Epic Progress Graph
Difficulty: 310
Copyright Nick Parlante 2017 - privacy