【leetCode】134_加油站

class Solution {
public:
    int canCompleteCircuit(vector& gas, vector& cost) {
        int left_oil = 0;
        int pos = 0;
        int num = gas.size();
        int iterations = num * 2;
        int start = 0, end = 0;
        for (int i = 0; i < num * 2; i ++){
            left_oil += gas[i % num];
            if (left_oil < cost[i % num]){
                left_oil = 0;
                start = (i + 1) % num;
                end = (i + 1) % num;
            }
            else{
                end ++;
                left_oil -= cost[i % num];
                if (end - start > num){
                    return start;
                }
            }
        }
        return -1;
    }
};

 

你可能感兴趣的:(leetCode)