Skip to content

Commit 2e5ccca

Browse files
committed
Time: 1 ms (85.79%), Space: 65.9 MB (51.85%) - LeetHub
source:5e02ea0ef73782212b41761db8d78bdb5a1cb5df
1 parent 8ec70cc commit 2e5ccca

File tree

1 file changed

+18
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)