Created
December 23, 2014 01:44
-
-
Save kanrourou/83cb2b06646ff301f20d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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