[Algo] utilización óptima

Dada 2 enumera  a y  b. Cada elemento es un par de enteros en el que el primer número entero representa el identificador único y el segundo número entero representa un valor. Su tarea es encontrar un elemento de  a y un formulario de elemento de  b tal manera que la suma de sus valores es menor o igual que  target y tan cerca  target como sea posible. Devolver una lista de identificadores de elementos seleccionados. Si ningún par es posible, devolver una lista vacía.

 

Ejemplo 1:

 

Input:
a = [[1, 2], [2, 4], [3, 6]] b = [[1, 2]] target = 7 Output: [[2, 1]] Explanation: There are only three combinations [1, 1], [2, 1], and [3, 1], which have a total sum of 4, 6 and 8, respectively. Since 6 is the largest sum that does not exceed 7, [2, 1] is the optimal pair. 

 

Ejemplo 2:

 

Input:
a = [[1, 3], [2, 5], [3, 7], [4, 10]]
b = [[1, 2], [2, 3], [3, 4], [4, 5]]
target = 10

Output: [[2, 4], [3, 2]]

Explanation:
There are two pairs possible. Element with id = 2 from the list `a` has a value 5, and element with id = 4 from the list `b` also has a value 5. Combined, they add up to 10. Similarily, element with id = 3 from `a` has a value 7, and element with id = 2 from `b` has a value 3. These also add up to 10. Therefore, the optimal pairs are [2, 4] and [3, 2]. 

 

Ejemplo 3:

 

Input:
a = [[1, 8], [2, 7], [3, 14]]
b = [[1, 5], [2, 10], [3, 14]]
target = 20

Output: [[3, 1]]

 

Ejemplo 4:

 

Input:
a = [[1, 8], [2, 15], [3, 9]]
b = [[1, 8], [2, 11], [3, 12]]
target = 20

Output: [[1, 3], [3, 2]]


    pública  estática List < int []> getOptimal (List < int []> aList, List < int []> ListaB, int objetivo) { 
        Lista < int []> res = nuevo ArrayList <> ();
        int max = Integer.MIN_VALUE; 
        Collections.sort (aList, ((a, b) -> a [1] - b [1 ])); 
        Collections.sort (ListaB, ((a, b) -> a [1] - b [1 ]));
        int i = 0, j = bList.size () - 1 ;
        para ( int [] arr: ListaB) {
            System.out.print (Arrays.toString (arr)); 
        }
        mientras que (i <aList.size () && j> = 0 ) {
             int suma = aList.get (i) [1] + bList.get (j) [1 ];
            si (suma> objetivo) { 
                j - = 1 ; 
            } Demás {
                 si (suma> max) {
                     // necesidad de actualización suma 
                    max = suma; 
                    res.clear (); 
                } 
                Res.add ( nuevo  int [] {aList.get (i) [0], bList.get (j) [0 ]});
                int index = j - 1 ;
                System.out.println ( "j:" + j); 
                System.out.println ( "índice:" + index);
                mientras que (index> = 0 && bList.get (índice) [1] == bList.get (índice + 1) [1 ]) { 
                    res.add ( nuevo  int [] {aList.get (i) [0], bList.get (índice) [0 ]}); 
                    índice - = 1 ; 
                } 
                I + = 1 ; 
            } 
        } 
        Volver res; 
    }

 

Supongo que te gusta

Origin www.cnblogs.com/xuanlu/p/12640374.html
Recomendado
Clasificación