加油站(gas station)

class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        int start = gas.size()-1;
        int end = 0;
        int sum = gas[start] - cost[start];
        while(start>end)
        {
            if(sum>=0)
            {
                sum +=gas[end]-cost[end];
                end++;
            }else{
                start--;
                sum+=gas[start]-cost[start];
            }
        }
        return sum>=0?start:-1;
    }
};
贪心算法

猜你喜欢

转载自blog.csdn.net/qq_22080999/article/details/80444691
今日推荐