File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
LeetcodeProblems/Algorithms/GasStation Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 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 ;
You can’t perform that action at this time.
0 commit comments