Skip to content

Instantly share code, notes, and snippets.

@hfknight
Last active August 29, 2015 14:06
Show Gist options
  • Save hfknight/777f4b00bc045837e02f to your computer and use it in GitHub Desktop.
Save hfknight/777f4b00bc045837e02f to your computer and use it in GitHub Desktop.
leetcode - gas station
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