Skip to content

Instantly share code, notes, and snippets.

@kanrourou
Created December 23, 2014 01:44
Show Gist options
  • Save kanrourou/83cb2b06646ff301f20d to your computer and use it in GitHub Desktop.
Save kanrourou/83cb2b06646ff301f20d to your computer and use it in GitHub Desktop.
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
if (gas == null || cost== null)
return -1;
int diff = 0;
int len = gas.length;
int start = 0;
for (int i = 0; i <= 2 * len - 1; i++) {
diff += (gas[i % len] - cost[i % len]);
if (diff < 0) {
if (i >= len - 1)
break;
else {
diff = 0;
start = i + 1;
}
} else {
if (i - start == len)
return i % len;
}
}
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment