Skip to content

Instantly share code, notes, and snippets.

@wwwmarcos
Created January 17, 2025 18:07
Show Gist options
  • Save wwwmarcos/ba8208ce710938c6998ce6fc14b3ed2a to your computer and use it in GitHub Desktop.
Save wwwmarcos/ba8208ce710938c6998ce6fc14b3ed2a to your computer and use it in GitHub Desktop.
const ingredientes = [
{ nome: 'Gin', sabor: 8, quantidade: 5 },
{ nome: 'Vermouth', sabor: 7, quantidade: 4 },
{ nome: 'Sugar', sabor: 5, quantidade: 3 },
{ nome: 'Lemon', sabor: 3, quantidade: 2 },
];
const maxQuantidade = 8;
function calcQuantidade (ingredientes) {
return ingredientes.reduce((prev, current) => prev + current.quantidade, 0)
}
function otimizarCoquetel(ingredientes, maxQuantidade, selected = []) {
const currentQuantidade = calcQuantidade(selected)
// 11
// sabor 8 -> quantidade 5
// sabor 16 -> quantidade 10
// 1
const menorQuantidade = findMenorQuantidade()
const restante = currentQuantidade - maxQuantidade
if (currentQuantidade < maxQuantidade && menorQuantidade >= restante) {
const ingredient = ingredientes.find(i => restante >= i.quantidade)
select.push(ingredient)
return otimizarCoquetel(ingredientes, maxQuantidade, selected)
}
return select
}
console.log(otimizarCoquetel(ingredientes.sort((a, b) => i.sabor ), maxQuantidade));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment