class Solution {
public int canCompleteCircuit ( int [ ] gas, int [ ] cost) {
int length = gas. length;
for ( int i = 0 ; i < length; i++ ) {
if ( gas[ i] >= cost[ i] ) {
int remain = 0 ;
int now = i;
for ( int j = 0 ; j < length; j++ ) {
remain = remain + gas[ now] - cost[ now] ;
if ( remain < 0 ) {
break ;
}
now++ ;
now = now % length;
}
if ( now == i) {
return i;
}
}
}
return - 1 ;
}
}
class Solution {
public static int canCompleteCircuit ( int [ ] gas, int [ ] cost) {
int length = gas. length;
int remain = 0 ;
int minRemain = Integer. MAX_VALUE;
int minIndex = 0 ;
for ( int i = 0 ; i < length; i++ ) {
remain += gas[ i] - cost[ i] ;
if ( remain < minRemain) {
minRemain = remain;
minIndex = i;
}
}
return remain >= 0 ? ( minIndex+ 1 ) % length : - 1 ;
}
}
class Solution {
public int canCompleteCircuit ( int [ ] gas, int [ ] cost) {
int length = gas. length;
for ( int i = 0 ; i < length; i++ ) {
int remain = 0 ;
int index = i;
int j;
for ( j = 0 ; j < length; j++ ) {
remain = remain + gas[ index % length] - cost[ index % length] ;
if ( remain < 0 ) {
break ;
}
index++ ;
}
if ( j == length) {
return i;
} else {
i = index;
}
}
return - 1 ;
}
}