Skip to content

Commit 076b489

Browse files
committed
update: 134
1 parent 276919a commit 076b489

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
4646
|127|[Word Ladder](https://leetcode.com/problems/word-ladder/) | [JavaScript](./src/word-ladder/res.js)|Medium|
4747
|130|[Surrounded Regions](https://leetcode.com/problems/surrounded-regions/) | [JavaScript](./src/surrounded-regions/res.js)|Medium|
4848
|133|[Clone Graph](https://leetcode.com/problems/clone-graph/) | [JavaScript](./src/clone-graph/res.js)|Medium|
49+
|134|[Gas Station](https://leetcode.com/problems/gas-station/) | [JavaScript](./src/gas-station/res.js)|Medium|
4950
|136|[Single Number](https://leetcode.com/problems/single-number/) | [JavaScript](./src/single-number/res.js)|Easy|
5051
|137|[Single Number II](https://leetcode.com/problems/single-number-ii/) | [JavaScript](./src/single-number-ii/res.js)|Medium|
5152
|151|[Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [JavaScript](./src/reverse-words-in-a-string/res.js)|Medium|

src/gas-station/res.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} gas
3+
* @param {number[]} cost
4+
* @return {number}
5+
*/
6+
var canCompleteCircuit = function(gas, cost) {
7+
let total_tank = 0, curr_tank = 0;
8+
let flag = 0;
9+
10+
for (let index = 0; index < gas.length; index++) {
11+
const remain = gas[index] - cost[index];
12+
13+
total_tank += remain;
14+
if (curr_tank + remain < 0) {
15+
flag = index+1;
16+
curr_tank = 0;
17+
} else {
18+
curr_tank += remain;
19+
}
20+
21+
}
22+
23+
if (total_tank < 0) return -1;
24+
25+
return flag;
26+
};

0 commit comments

Comments
 (0)