Skip to content

Commit 1d77fbb

Browse files
authored
Create 134-gas-station.js
1 parent 474880c commit 1d77fbb

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

134-gas-station.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} gas
3+
* @param {number[]} cost
4+
* @return {number}
5+
*/
6+
const canCompleteCircuit = function(gas, cost) {
7+
let total = 0
8+
let curLeft = 0
9+
let curtIdx = 0
10+
for (let i = 0; i < gas.length; i++) {
11+
total += gas[i] - cost[i]
12+
curLeft += gas[i] - cost[i]
13+
if (curLeft < 0) {
14+
curtIdx = i + 1
15+
curLeft = 0
16+
}
17+
}
18+
return total < 0 ? -1 : curtIdx
19+
}

0 commit comments

Comments
 (0)