Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7073529

Browse files
Add solution to 134.
1 parent 687b042 commit 7073529

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

‎solutions/0134.gas-station.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {number[]} gas
3+
* @param {number[]} cost
4+
* @return {number}
5+
*/
6+
var canCompleteCircuit = function (gas, cost) {
7+
let n = gas.length;
8+
let totalGas = 0,
9+
currentGas = 0;
10+
let startAt = 0;
11+
12+
for (let i = 0; i < n; i++) {
13+
totalGas += gas[i] - cost[i];
14+
currentGas += gas[i] - cost[i];
15+
16+
if (currentGas < 0) {
17+
startAt = i + 1;
18+
currentGas = 0;
19+
}
20+
}
21+
22+
if (totalGas >= 0) {
23+
return startAt;
24+
}
25+
26+
return -1;
27+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /