public class Solution {
//解法一:贪心算法求解
public int canCompleteCircuit(int[] gas, int[] cost) {
int total=0,sum=0;
int index=-1;
for(int i=0;i!=gas.length;i++)
{
sum+=gas[i]-cost[i];
total+=gas[i]-cost[i];
if(sum<0)
{
sum=0;
index=i;
}
}
return total>=0?index+1:-1;
}
public static void main(String[]args){
//System.out.println("Hello World!");
int[] gas={2,5,7,6};
int[] cost={1,6,6,3};
Solution s=new Solution();
System.out.println(s.canCompleteCircuit(gas,cost));
}
}
汽车是否能围绕一圈行走
最新推荐文章于 2024-08-23 23:33:37 发布