Gas Station

 

    int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        int tank = 0;
        int total = 0;
        int n = gas.size();
        int index = -1;
        for(int i=0;i<n;i++)
        {
            total += (gas[i]-cost[i]);
            tank += (gas[i]-cost[i]);
            if(tank<0)
            {
                tank = 0;
                index = i;
            }
        }
        
        return (total>=0?index+1:-1);
        
        
    }

  

posted @ 2013-10-11 22:01  summer_zhou  阅读(168)  评论(0编辑  收藏  举报