- Notifications
You must be signed in to change notification settings - Fork 117
/
Copy path134.c
31 lines (25 loc) · 693 Bytes
/
134.c
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>
intcanCompleteCircuit(int*gas, intgasSize, int*cost, intcostSize) {
if (gasSize!=costSize) return-1;
inti;
inttank=0, sum=0;
intans=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;
}
}
returntank >= 0 ? ans : -1;
}
intmain() {
intgas[] = { 1, 2 };
intcost[] = { 2, 1 };
assert(canCompleteCircuit(gas, sizeof(gas) / sizeof(gas[0]),
cost, sizeof(cost) / sizeof(cost[0])) ==1);
printf("all tests passed!\n");
return0;
}