Skip to content

Instantly share code, notes, and snippets.

@technikhil314
Created April 23, 2022 03:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save technikhil314/c505eb34bde72acce9be2f48c9ec7ca0 to your computer and use it in GitHub Desktop.
Save technikhil314/c505eb34bde72acce9be2f48c9ec7ca0 to your computer and use it in GitHub Desktop.
Amazon optimizing box weight
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