-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy path134.cpp
51 lines (50 loc) · 1.38 KB
/
134.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
__________________________________________________________________________________________________
4ms
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int total = 0;
int minc = 0;
int inx = -1;
for(int i = 0; i < gas.size(); i++){
total += gas[i] - cost[i];
if(total < minc){
minc = total;
inx = i;
}
}
if(total < 0) return -1;
else return (inx+1)%gas.size();
}
};
static int x = [](){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return 0;
}();
__________________________________________________________________________________________________
8792 kb
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int st = 0;
int cur = st;
int tank = 0;
int len = gas.size();
while(true){
tank = tank+gas[cur]-cost[cur];
cur = (cur+1)%len;
if(tank >= 0){ //解的唯一性
if(cur == st) return st;
}else{
if(cur <= st) return -1;
else{
st = cur;
tank = 0;
}
}
}
}
};
__________________________________________________________________________________________________