Skip to content

Commit c7bf975

Browse files
deepesh85bignacio-chiazzo
deepesh85b
authored andcommitted
Added Solution of Gas Station (JavaScript)
1 parent 04a3e81 commit c7bf975

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Problem: https://leetcode.com/problems/gas-station/description/
3+
*/
4+
/**
5+
* @param {number[]} gas
6+
* @param {number[]} cost
7+
* @return {number}
8+
*/
9+
var canCompleteCircuit = function(gas, cost) {
10+
var gasSum =0, costSum = 0;
11+
var len = gas.length;
12+
var result = 0;
13+
14+
for (var i = 0; i < len; ++i) {
15+
gasSum += gas[i];
16+
costSum += cost[i];
17+
}
18+
19+
if (costSum > gasSum)
20+
return -1;
21+
else {
22+
gasSum = costSum = 0;
23+
for (var i = 0; i < len; ++i) {
24+
gasSum += gas[i];
25+
costSum += cost[i];
26+
if (costSum > gasSum) {
27+
gasSum = 0;
28+
costSum = 0;
29+
result = i + 1;
30+
}
31+
}
32+
}
33+
34+
return result;
35+
};
36+
module.exports = canCompleteCircuit;

0 commit comments

Comments
 (0)