-
Notifications
You must be signed in to change notification settings - Fork 77
/
solution.cpp
42 lines (42 loc) · 980 Bytes
/
solution.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
class Solution
{
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost)
{
vector<int> remainder;
int sum =0;
for(int i = 0; i < gas.size(); i++)
{
remainder.push_back(gas[i]-cost[i]);
sum += gas[i]-cost[i];
}
if(sum < 0)
{
return -1;
}
else
{
int start;
int cur = 0;
do
{
start = cur;
int tmp = remainder[cur++];
while(tmp >= 0 && cur<gas.size())
{
tmp += remainder[cur++];
if(tmp < 0)
{
break;
}
}
if(tmp >= 0 && cur == gas.size())
{
return start;
}
}
while(cur<gas.size());
return -1;
}
}
};