-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGasStations.cpp
49 lines (48 loc) · 1.31 KB
/
GasStations.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
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int i,sz=gas.size(),current_tank=0,total_tank=0,start=0;
for(i=0;i<sz;i++)
{
total_tank=total_tank+gas[i]-cost[i];
current_tank=current_tank+gas[i]-cost[i];
if(current_tank<0)
{
current_tank=0;
start=i+1;
}
}
return total_tank>=0 ? start:-1;
}
};
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int i,sz=gas.size(),totalgas=0,totalcost=0,flag,pos,start;
for(i=0;i<sz;i++)
{
flag=0;
if(gas[i]>=cost[i])
{
start=i;
totalgas=gas[i]-cost[i];
pos=(start+1)%sz;
while(pos!=start)
{
totalgas=totalgas+gas[pos]-cost[pos];
if(totalgas<0)
{
flag=1;
break;
}
pos=(pos+1)%sz;
}
if((pos==start) && ((totalgas)>=0))
return start;
else if(flag==1)
continue;
}
}
return -1;
}
};