Last active
March 21, 2019 13:09
-
-
Save szabi84/64d3d86687f1b89b58714f5400dcd4bc to your computer and use it in GitHub Desktop.
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'; | |
const buckets = (buckets) => { | |
let sum = 0; | |
let times = 0; | |
for (let i = Math.min(...buckets); i < Math.max(...buckets); i++) { | |
let start = false; | |
let subTotal = 0; | |
for (const bucket of buckets) { | |
times++; | |
if (start && bucket < i + 1) { | |
subTotal++; | |
} | |
if (start && bucket > i) { | |
start = false; | |
sum += subTotal; | |
subTotal = 0; | |
} | |
if (bucket > i) { | |
start = true; | |
} | |
} | |
} | |
console.log(`The algorithm runs on ${buckets.length} length array ${times} times`); | |
return sum; | |
}; | |
//Test | |
const array = []; | |
array.push([3, 2, 3]); | |
array.push([2, 1, 1]); | |
array.push([1, 1, 2]); | |
array.push([1, 2, 1, 3, 1, 2]); | |
array.push([3, 1, 2, 1, 3, 1]); | |
array.push([3, 1, 2, 1]); | |
array.push([5, 2, 3, 4, 2, 2]); | |
array.forEach((element) => { | |
console.log("Input array: ", element); | |
console.log("sum of bucket units: ", buckets(element)); | |
console.log("--------------------------------------------------"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment