private List < Container > filterByVolumeAndWeight ( List < Box > boxes , List < Container > containers , int count ) { long volume = 0 ; long minVolume = Long . MAX_VALUE ; long weight = 0 ; long minWeight = Long . MAX_VALUE ; for ( Box box : boxes ) { // volume long boxVolume = box . getVolume ( ) ; volume += boxVolume ; if ( boxVolume < minVolume ) { minVolume = boxVolume ; } // weight long boxWeight = box . getWeight ( ) ; weight += boxWeight ; if ( boxWeight < minWeight ) { minWeight = boxWeight ; } } long maxVolume = Long . MIN_VALUE ; long maxWeight = Long . MIN_VALUE ; for ( Container container : containers ) { // volume long boxVolume = container . getVolume ( ) ; if ( boxVolume > maxVolume ) { maxVolume = boxVolume ; } // weight long boxWeight = container . getWeight ( ) ; if ( boxWeight > maxWeight ) { maxWeight = boxWeight ; } } if ( maxVolume * count < volume || maxWeight * count < weight ) { // no containers will work at current count return Collections . emptyList ( ) ; } List < Container > list = new ArrayList <> ( containers . size ( ) ) ; for ( Container container : containers ) { if ( container . getVolume ( ) < minVolume || container . getWeight ( ) < minWeight ) { // this box cannot even fit a single box continue ; } if ( container . getVolume ( ) + maxVolume * ( count - 1 ) < volume || container . getWeight ( ) + maxWeight * ( count - 1 ) < weight ) { // this box cannot be used even together with all biggest boxes continue ; } if ( count == 1 ) { if ( ! canHoldAll ( container , boxes ) ) { continue ; } } else { if ( ! canHoldAtLeastOne ( container , boxes ) ) { continue ; } } list . add ( container ) ; } return list ; }
Return a list of containers which can potentially hold the boxes .