大卡车侧翻压扁小车# Joke - 肚皮舞运动
y*o
1 楼
1 loop method:
class Solution {
public:
int jump(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(n<=1){
return 0;
}
int iend=A[0];
int stepCount=1;
int nextEnd=A[0];
int i=1;
while(i if(iendreturn -1;
}
while(i<=iend){
if(nextEndnextEnd=i+A[i];
}
++i;
}
stepCount++;
iend=nextEnd;
}
return stepCount;
}
};
应该比下面的greedy好,因为只有一次遍历
greedy?
int jump(int A[], int n) {
int hops = 0;
int end = n-1;
while(end)
{
for(int i = 0; i <= end; i++)
{
if((A[i]+i)>=end)
{
hops++;
end = i;
}
}
}
return hops;
}
class Solution {
public:
int jump(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(n<=1){
return 0;
}
int iend=A[0];
int stepCount=1;
int nextEnd=A[0];
int i=1;
while(i
}
while(i<=iend){
if(nextEndnextEnd=i+A[i];
}
++i;
}
stepCount++;
iend=nextEnd;
}
return stepCount;
}
};
应该比下面的greedy好,因为只有一次遍历
greedy?
int jump(int A[], int n) {
int hops = 0;
int end = n-1;
while(end)
{
for(int i = 0; i <= end; i++)
{
if((A[i]+i)>=end)
{
hops++;
end = i;
}
}
}
return hops;
}