环形路加油

 

#include <vector>
using namespace std;

int selectGasStation(const vector<int> &a, const vector<int> &g) {
    int total = 0;
    int sum = 0;
    int n = g.size();
    int index = -1;
    for(int i=0;i<n;i++)
    {
        total += (a[i]-g[i]);
        sum += (a[i]-g[i]);
        if(sum<0)
        {
            index = i;
            sum = 0;
        }
    }
    return (total>=0?index+1:-1);
}

  

posted @ 2013-10-13 11:07  summer_zhou  阅读(229)  评论(0编辑  收藏  举报