1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-16 16:36:56 +02:00
LeetCode/kt/maximum-units-on-a-truck.kt
Matej Focko aaaebf1d52
style(kt): reformat the files
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-05-17 18:23:38 +02:00

23 lines
679 B
Kotlin

class Solution {
data class BoxType(val boxes: Int, val units: Int)
fun toBoxType(x: IntArray): BoxType = BoxType(x[0], x[1])
fun maximumUnits(
boxTypes: Array<IntArray>,
truckSize: Int,
): Int =
boxTypes
.map { toBoxType(it) }
.sortedByDescending(BoxType::units)
.fold(0 to 0) { acc, boxType ->
if (acc.first < truckSize) {
val count = minOf(truckSize - acc.first, boxType.boxes)
(acc.first + count) to (acc.second + count * boxType.units)
} else {
acc
}
}
.second
}