-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path134.gas-station.js
63 lines (51 loc) · 1.47 KB
/
134.gas-station.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* @lc app=leetcode id=134 lang=javascript
*
* [134] Gas Station
*/
/**
* @param {number[]} gas
* @param {number[]} cost
* @return {number}
*/
/**
* @param {number[]} gas
* @param {number[]} cost
* @return {number}
*/
/*
gas=[numberOfGasWillGotAtThisStation]
cost=[numberOfGasForLeavingThisStationAndGoToNextStation]
*/
/*
Potential solution, trial and error for by loop. n^n
If totalCost is greater than gas available then return -1
If not, we know there's guaranteed solution so just go through and return start
https://leetcode.com/problems/gas-station/discuss/276561/Super-Simple-JavaScript-solution-(with-comments)
*/
var canCompleteCircuit = function(gas, cost) {
let sGas = gas.reduce((sum, item) => sum + item, 0);
let sCst = cost.reduce((sum, item) => sum + item, 0);
//expr = return stmt
//because it must be an close back cirlcle, so it must be bigger
if (sGas < sCst) return -1;
let len = gas.length,
gasGainByDeductionOfCost = 0,
loc = 0;
/*
Honestly very simple data structure, the procedure must have do some tricks
guaranteed to be unique & Circle solution
that's why finding the first leave can win+freedom to choice first position
problem space n
*/
for (let i = 0; i < len; i++) {
gasGainByDeductionOfCost += gas[i] - cost[i];
if (gasGainByDeductionOfCost < 0) {
//failure reset
gasGainByDeductionOfCost = 0;
loc = i + 1;
console.log(i, loc, gasGainByDeductionOfCost);
}
}
return loc;
};