Created
April 23, 2022 03:34
-
-
Save technikhil314/c505eb34bde72acce9be2f48c9ec7ca0 to your computer and use it in GitHub Desktop.
Amazon optimizing box weight
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
function minimalHeaviestSetA(arr) { | |
let result = []; | |
const sum = (arr) => arr.reduce((acc, curr) => acc + curr, 0); | |
const sortedArr = [...arr].sort((x, y) => x - y > 0 ? -1 : 1); | |
const n = sortedArr.length; | |
const target = sum(arr) / 2 | |
for(let i = 0; i < n; i++) { | |
if(sum(result) > target) { | |
break | |
} | |
result[i] = sortedArr[i]; | |
} | |
return result.reverse(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment