bool canJump(vector<int>& nums) {
int last = nums.size() - 1;
function<bool(int)> reach = [&](int i) -> bool {
if (i >= last) return true;
for (int step = 1; step <= nums[i]; step++) {
if (reach(i + step)) return true;
}
return false;
};
return reach(0);
}
public boolean canJump(int[] nums) {
int last = nums.length - 1;
return reach(nums, last, 0);
}
private boolean reach(int[] nums, int last, int i) {
if (i >= last) return true;
for (int step = 1; step <= nums[i]; step++) {
if (reach(nums, last, i + step)) return true;
}
return false;
}
def can_jump(nums):
last = len(nums) - 1
def reach(i):
if i >= last: return True
for step in range(1, nums[i] + 1):
if reach(i + step): return True
return False
return reach(0)
function canJump(nums: number[]): boolean {
const last = nums.length - 1;
const reach = (i: number): boolean => {
if (i >= last) return true;
for (let step = 1; step <= nums[i]; step++) {
if (reach(i + step)) return true;
}
return false;
};
return reach(0);
}
bool canJump(vector<int>& nums) {
int last = nums.size() - 1;
vector<int> memo(nums.size(), -1);
function<bool(int)> reach = [&](int i) -> bool {
if (i >= last) return true;
if (memo[i] != -1) return memo[i];
bool ok = false;
for (int step = 1; step <= nums[i]; step++) {
if (reach(i + step)) {
ok = true;
break;
}
}
memo[i] = ok;
return ok;
};
return reach(0);
}
public boolean canJump(int[] nums) {
int last = nums.length - 1;
int[] memo = new int[nums.length];
Arrays.fill(memo, -1);
return reach(nums, memo, last, 0);
}
private boolean reach(int[] nums, int[] memo, int last, int i) {
if (i >= last) return true;
if (memo[i] != -1) return memo[i] == 1;
boolean ok = false;
for (int step = 1; step <= nums[i]; step++) {
if (reach(nums, memo, last, i + step)) {
ok = true;
break;
}
}
memo[i] = ok ? 1 : 0;
return ok;
}
def can_jump(nums):
last = len(nums) - 1
memo = {}
def reach(i):
if i >= last: return True
if i in memo: return memo[i]
ok = False
for step in range(1, nums[i] + 1):
if reach(i + step):
ok = True
break
memo[i] = ok
return ok
return reach(0)
function canJump(nums: number[]): boolean {
const last = nums.length - 1;
const memo = new Array<boolean | undefined>(nums.length);
const reach = (i: number): boolean => {
if (i >= last) return true;
if (memo[i] !== undefined) return memo[i] as boolean;
let ok = false;
for (let step = 1; step <= nums[i]; step++) {
if (reach(i + step)) {
ok = true;
break;
}
}
memo[i] = ok;
return ok;
};
return reach(0);
}
bool canJump(vector<int>& nums) {
int n = nums.size();
vector<bool> good(n, false);
good[n - 1] = true;
for (int i = n - 2; i >= 0; i--) {
int furthest = min(i + nums[i], n - 1);
for (int j = i + 1; j <= furthest; j++) {
if (good[j]) {
good[i] = true;
break;
}
}
}
return good[0];
}
public boolean canJump(int[] nums) {
int n = nums.length;
boolean[] good = new boolean[n];
good[n - 1] = true;
for (int i = n - 2; i >= 0; i--) {
int furthest = Math.min(i + nums[i], n - 1);
for (int j = i + 1; j <= furthest; j++) {
if (good[j]) {
good[i] = true;
break;
}
}
}
return good[0];
}
def can_jump(nums):
n = len(nums)
good = [False] * n
good[n - 1] = True
for i in range(n - 2, -1, -1):
furthest = min(i + nums[i], n - 1)
for j in range(i + 1, furthest + 1):
if good[j]:
good[i] = True
break
return good[0]
function canJump(nums: number[]): boolean {
const n = nums.length;
const good = new Array<boolean>(n).fill(false);
good[n - 1] = true;
for (let i = n - 2; i >= 0; i--) {
const furthest = Math.min(i + nums[i], n - 1);
for (let j = i + 1; j <= furthest; j++) {
if (good[j]) {
good[i] = true;
break;
}
}
}
return good[0];
}
bool canJump(vector<int>& nums) {
int farthest = 0;
for (int i = 0; i < (int)nums.size(); i++) {
if (i > farthest) return false;
farthest = max(farthest, i + nums[i]);
if (farthest >= (int)nums.size() - 1) return true;
}
return true;
}
public boolean canJump(int[] nums) {
int farthest = 0;
for (int i = 0; i < nums.length; i++) {
if (i > farthest) return false;
farthest = Math.max(farthest, i + nums[i]);
if (farthest >= nums.length - 1) return true;
}
return true;
}
def can_jump(nums):
farthest = 0
for i in range(len(nums)):
if i > farthest: return False
farthest = max(farthest, i + nums[i])
if farthest >= len(nums) - 1: return True
return True
function canJump(nums: number[]): boolean {
let farthest = 0;
for (let i = 0; i < nums.length; i++) {
if (i > farthest) return false;
farthest = Math.max(farthest, i + nums[i]);
if (farthest >= nums.length - 1) return true;
}
return true;
}