Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 99e082e

Browse files
authoredOct 15, 2019
Update 134-gas-station.js
1 parent 1d77fbb commit 99e082e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
 

‎134-gas-station.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,23 @@ const canCompleteCircuit = function(gas, cost) {
1717
}
1818
return total < 0 ? -1 : curtIdx
1919
}
20+
21+
// another
22+
23+
const canCompleteCircuit = function(gas, cost) {
24+
const len = gas.length
25+
let tank = 0
26+
let count = 0
27+
for (let i = 0; i < len * 2; i++) {
28+
let idx = i % len
29+
if (count === len) return idx
30+
count += 1
31+
tank += gas[idx] - cost[idx]
32+
if (tank < 0) {
33+
tank = 0
34+
count = 0
35+
}
36+
}
37+
return -1
38+
}
39+

0 commit comments

Comments
 (0)
Please sign in to comment.