Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4],
return true.
A = [3,2,1,0,4],
return false.
class Solution {
public:
bool canJump(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int maxx=0;
for(int i=0;i<n;++i)
{
if(i<=maxx)
{
maxx=max(maxx,i+A[i]);
}
}
return maxx>=n-1;
}
};

本文讨论了如何使用贪心算法解决跳远游戏问题,即在一个数组中找到从第一个元素开始,是否可以通过跳跃到达最后一个元素。通过迭代计算每个位置可以达到的最远距离,最终判断是否能够到达终点。
1083

被折叠的 条评论
为什么被折叠?



