Last active
August 29, 2015 14:06
-
-
Save hfknight/777f4b00bc045837e02f to your computer and use it in GitHub Desktop.
leetcode - gas station
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int canCompleteCircuit(int[] gas, int[] cost) { | |
int[] remain = new int[gas.length]; | |
for (int i = 0; i < remain.length; i++) { | |
int j = i; | |
boolean canComp = true; | |
int capt = 0; | |
do { | |
capt += gas[j] - cost[j]; | |
if (capt < 0) { | |
canComp = false; | |
break; | |
} | |
j++; | |
if (j == remain.length) | |
j = 0; | |
} while (j != i); | |
if (canComp) | |
return i; | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment