Created
April 3, 2019 05:36
-
-
Save aire-con-gas/1ce3fa495c0de67e72adfa4ba260e2ba to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/gokacel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
"use strict"; | |
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } } | |
var computeMinRefills = function computeMinRefills(dist, tank, stops) { | |
var combinedStops = [0].concat(_toConsumableArray(stops), [dist]); | |
var distanceTravelled = 0; | |
var currentIdx = 0; | |
var j = undefined; | |
var numOfRefills = 0; | |
while (distanceTravelled <= dist) { | |
j = currentIdx + 1; | |
if (combinedStops[j] >= dist) { | |
break; | |
} | |
if (combinedStops[j] - combinedStops[currentIdx] > tank) { | |
return -1; | |
} | |
while (combinedStops[j] - combinedStops[currentIdx] <= tank) { | |
j++; | |
} | |
distanceTravelled += combinedStops[j - 1] - combinedStops[currentIdx]; | |
numOfRefills++; | |
currentIdx = j - 1; | |
} | |
return numOfRefills; | |
}; | |
console.log(computeMinRefills(950, 400, [200, 375, 550, 750])); | |
console.log(computeMinRefills(10, 3, [1, 2, 5, 9])); | |
// 950 | |
// 400 | |
// [200 375 550 750] | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const computeMinRefills = (dist, tank, stops) => { | |
const combinedStops = [0, ...stops, dist]; | |
let distanceTravelled = 0; | |
let currentIdx = 0; | |
let j; | |
let numOfRefills = 0; | |
while(distanceTravelled <= dist) { | |
j = currentIdx + 1; | |
if (combinedStops[j] >= dist) { | |
break; | |
} | |
if (combinedStops[j] - combinedStops[currentIdx] > tank) { | |
return -1; | |
} | |
while (combinedStops[j] - combinedStops[currentIdx] <= tank) { | |
j++; | |
} | |
distanceTravelled += combinedStops[j - 1] - combinedStops[currentIdx]; | |
numOfRefills++; | |
currentIdx = j - 1; | |
} | |
return numOfRefills; | |
} | |
console.log(computeMinRefills(950, 400, [200,375,550,750])); | |
console.log(computeMinRefills(10, 3, [1,2,5,9])); | |
// 950 | |
// 400 | |
// [200 375 550 750] | |
</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } } | |
var computeMinRefills = function computeMinRefills(dist, tank, stops) { | |
var combinedStops = [0].concat(_toConsumableArray(stops), [dist]); | |
var distanceTravelled = 0; | |
var currentIdx = 0; | |
var j = undefined; | |
var numOfRefills = 0; | |
while (distanceTravelled <= dist) { | |
j = currentIdx + 1; | |
if (combinedStops[j] >= dist) { | |
break; | |
} | |
if (combinedStops[j] - combinedStops[currentIdx] > tank) { | |
return -1; | |
} | |
while (combinedStops[j] - combinedStops[currentIdx] <= tank) { | |
j++; | |
} | |
distanceTravelled += combinedStops[j - 1] - combinedStops[currentIdx]; | |
numOfRefills++; | |
currentIdx = j - 1; | |
} | |
return numOfRefills; | |
}; | |
console.log(computeMinRefills(950, 400, [200, 375, 550, 750])); | |
console.log(computeMinRefills(10, 3, [1, 2, 5, 9])); | |
// 950 | |
// 400 | |
// [200 375 550 750] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment