记录最远可以到达的地方,在遍历每个nums时,更新最远的距离
#include <iostream>
using namespace std;
const int N = 2e5+10;
int a[N];
int n;
int main() {
scanf("%d",&n);
for(int i = 0; i < n; i++){
scanf("%d", &a[i]);
}
//最远位置
int farthest = 0;
bool ans = true;
for(int i = 0; i < n; i++){
if(farthest>=i && a[i]+i>farthest){
farthest = a[i]+i;
}else if(farthest < i){
ans = false;
break;
}
}
if(ans)
printf("true");
else
printf("false");
return 0;
}
// 64 位输出请用 printf("%lld")

京公网安备 11010502036488号