Skip to content

Commit d55c602

Browse files
committed
update: 134
1 parent d34ae34 commit d55c602

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
8383
| 130 | [Surrounded Regions](https://leetcode.com/problems/surrounded-regions/) | [JavaScript](./src/surrounded-regions/res.js) | Medium |
8484
| 131 | [palindrome-partitioning](https://leetcode.com/problems/palindrome-partitioning/) | [TypeScript](./src/palindrome-partitioning/res.ts) | Medium |
8585
| 133 | [Clone Graph](https://leetcode.com/problems/clone-graph/) | [JavaScript](./src/clone-graph/res.js) | Medium |
86-
| 134 | [Gas Station](https://leetcode.com/problems/gas-station/) | [JavaScript](./src/gas-station/res.js) | Medium |
86+
| 134 | [Gas Station](https://leetcode.com/problems/gas-station/) | [JavaScript](./src/gas-station/res.js) · [TypeScript](./src/gas-station/res.ts) | Medium |
8787
| 135 | [Candy](https://leetcode.com/problems/candy/) | [JavaScript](./src/candy/res.js) | Hard |
8888
| 136 | [Single Number](https://leetcode.com/problems/single-number/) | [JavaScript](./src/single-number/res.js) | Easy |
8989
| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [JavaScript](./src/single-number-ii/res.js) | Medium |

src/gas-station/res.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function canCompleteCircuit(gas: number[], cost: number[]): number {
2+
const rest = gas.map((gasItem, i) => gasItem - cost[i]);
3+
let remain = 0;
4+
let startIndex = 0;
5+
6+
if (rest.reduce((p,c) => p+c, 0) < 0) {
7+
return -1;
8+
}
9+
10+
let count = rest.length;
11+
for (let i = 0; ; ) {
12+
if (remain + rest[i] < 0) {
13+
remain = 0;
14+
i = (i+1) % rest.length;
15+
startIndex = i;
16+
count =rest.length;
17+
} else {
18+
remain += rest[i];
19+
i = (i+1) % rest.length;
20+
count--;
21+
22+
if (!count && remain >= 0) {
23+
break;
24+
}
25+
}
26+
}
27+
28+
return startIndex;
29+
};

0 commit comments

Comments
 (0)