mirror of
https://github.com/lWolvesl/leetcode.git
synced 2026-01-12 18:08:38 +08:00
22 lines
551 B
C
22 lines
551 B
C
int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize) {
|
|
int i = 0;
|
|
while (i < gasSize) {
|
|
int sumOfGas = 0, sumOfCost = 0;
|
|
int cnt = 0;
|
|
while (cnt < gasSize) {
|
|
int j = (i + cnt) % gasSize;
|
|
sumOfGas += gas[j];
|
|
sumOfCost += cost[j];
|
|
if (sumOfCost > sumOfGas) {
|
|
break;
|
|
}
|
|
cnt++;
|
|
}
|
|
if (cnt == gasSize) {
|
|
return i;
|
|
} else {
|
|
i = i + cnt + 1;
|
|
}
|
|
}
|
|
return -1;
|
|
} |