We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 04a3e81 commit c7bf975Copy full SHA for c7bf975
LeetcodeProblems/Algorithms/GasStation/gasStation.js
@@ -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
24
25
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