![JAR search and dependency download from the Maven repository](/logo.png)
g1701_1800.s1710_maximum_units_on_a_truck.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
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