Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

406
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda