134. Gas Station

class Solution {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int total=0,cur=0,start=0;
        for(int i=0;i<gas.length;i++)
        {
            cur+=gas[i]-cost[i];
            total+=gas[i]-cost[i];
            if(cur<0)
            {
                cur=0;
                start=i+1;
            }
        }
        return total>=0?start:-1;
    }
}

  

posted @ 2017-10-07 05:07  Weiyu Wang  阅读(124)  评论(0编辑  收藏  举报