Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

408
Visualizações
Javascript Algorithm to find the best combination(least number of items) of lengths less than a given value

How to find the best combination of n values which gives the least amount of sum ( n1+n2+n3+n4+n5)

maxDiff = D
requiredLength = L

lengthArray = [l1, l2, l3, l4, l5, l6]

1st constraint,
diff = L - (n1*l1 + n2*l2 + n3*l3 + n4*l4 + n5*l5)

2nd constraint,
0 >= diff <= D

Here, l are different length values of sheets ( 1000mm, 1100mm, ..., 2000mm etc) L is the maximum length required (entered by an user). i want to calculate the best combination of n values(>=0) which basically returns the least number of sheets.

if there is a diff, this can be the gap between sheets. (it can also be 0)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can find solution with dynamic programming

Make array A of size L+1 containing item counters (initially 0) and the last item added.

For every item length li walk through array from index range k: from A[i] to A[l]

If adding item li to A[k-li] makes count in the cell A[k] better (lower), replace current value in A[k]. Pseudocode:

if (A[k-l[i]].count + 1 < A[k].count) 
   {
     A[k].count = A[k-l[i]].count + 1; 
     A[k].item =  l[i];
   }

Finally check if A[L] contains item set. If now, walk down to get closest result.

To retrieve items, go down by A[k].item steps

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda