All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g1701_1800.s1710_maximum_units_on_a_truck.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.32
Show newest version
package g1701_1800.s1710_maximum_units_on_a_truck

// #Easy #Array #Sorting #Greedy #2023_06_16_Time_228_ms_(100.00%)_Space_38.5_MB_(100.00%)

import java.util.Arrays

@Suppress("NAME_SHADOWING")
class Solution {
    fun maximumUnits(boxTypes: Array, truckSize: Int): Int {
        var truckSize = truckSize
        Arrays.sort(boxTypes) { b1: IntArray, b2: IntArray -> Integer.compare(b2[1], b1[1]) }
        var maxUnits = 0
        var i = 0
        while (truckSize > 0 && i < boxTypes.size) {
            if (boxTypes[i][0] <= truckSize) {
                maxUnits += boxTypes[i][0] * boxTypes[i][1]
                truckSize -= boxTypes[i][0]
            } else {
                maxUnits += Math.min(truckSize, boxTypes[i][0]) * boxTypes[i][1]
                truckSize -= Math.min(truckSize, boxTypes[i][0])
            }
            i++
        }
        return maxUnits
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy