Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6121422

Browse files
committed
"Gas Station"
1 parent b93a4bf commit 6121422

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ The `☢` means that you need to have a LeetCode Premium Subscription.
166166
| 137 | [Single Number II] | |
167167
| 136 | [Single Number] | [C](src/136.c) |
168168
| 135 | [Candy] | |
169-
| 134 | [Gas Station] | |
169+
| 134 | [Gas Station] | [C](src/134.c) |
170170
| 133 | [Clone Graph] | [C++](src/133.cpp) |
171171
| 132 | [Palindrome Partitioning II] | |
172172
| 131 | [Palindrome Partitioning] | |

‎src/134.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <stdio.h>
2+
#include <assert.h>
3+
4+
int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize) {
5+
if (gasSize != costSize) return -1;
6+
int i;
7+
int tank = 0, sum = 0;
8+
int ans = 0;
9+
for (i = 0; i < gasSize; i++) {
10+
sum += gas[i] - cost[i];
11+
tank += gas[i] - cost[i];
12+
if (sum < 0) {
13+
ans = i + 1;
14+
sum = 0;
15+
}
16+
}
17+
18+
return tank >= 0 ? ans : -1;
19+
}
20+
21+
int main() {
22+
int gas[] = { 1, 2 };
23+
int cost[] = { 2, 1 };
24+
25+
assert(canCompleteCircuit(gas, sizeof(gas) / sizeof(gas[0]),
26+
cost, sizeof(cost) / sizeof(cost[0])) == 1);
27+
28+
printf("all tests passed!\n");
29+
30+
return 0;
31+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /