forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path134.c
More file actions
31 lines (25 loc) · 693 Bytes
/
134.c
File metadata and controls
31 lines (25 loc) · 693 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include <assert.h>
int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize) {
if (gasSize != costSize) return -1;
int i;
int tank = 0, sum = 0;
int ans = 0;
for (i = 0; i < gasSize; i++) {
sum += gas[i] - cost[i];
tank += gas[i] - cost[i];
if (sum < 0) {
ans = i + 1;
sum = 0;
}
}
return tank >= 0 ? ans : -1;
}
int main() {
int gas[] = { 1, 2 };
int cost[] = { 2, 1 };
assert(canCompleteCircuit(gas, sizeof(gas) / sizeof(gas[0]),
cost, sizeof(cost) / sizeof(cost[0])) == 1);
printf("all tests passed!\n");
return 0;
}