点击打开链接
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <string.h>
#include <algorithm>
#include <vector>
#include <numeric>
#include <limits>
#include <math.h>
#include <queue>
using namespace std;
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int length=gas.size();
int sum=0, total=0, j=-1;
for(int i=0;i<length;i++){
sum+=gas[i]-cost[i];
total+=gas[i]-cost[i];
if(sum<0){
j=i;//表明从下一个j+1开始,如果total>0 就是正的
sum=0;
}
}
if(total<0){ //负圈
return -1;
}
else{
return j+1;
}
}
};
int main()
{
return 0;
}
// total是全局,sum是局部