-
Notifications
You must be signed in to change notification settings - Fork 1
/
134. Gas Station
50 lines (49 loc) · 1.04 KB
/
134. Gas Station
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
class Solution {
public int canCompleteCircuit(int[] gas, int[] cost)
{
int n=gas.length;
int i,j,a,f,x=1;
if(gas==cost)
return 0;
for(i=0;i<n;i++)
{
if(gas[i]<cost[i])
{
x=0;
continue;
}
else if(gas[i]==cost[i])
continue;
x=0;
a=0;
f=0;
for(j=i;j<n;j++)
{
a+=gas[j];
a-=cost[j];
if(a<0)
{
f=1;
break;
}
}
if(f==1)
continue;
for(j=0;j<i;j++)
{
a+=gas[j];
a-=cost[j];
if(a<0)
{
f=1;
break;
}
}
if(f==0)
return i;
}
if(x==1)
return 0;
return -1;
}
}