LeetCode 1710. Maximum Units on a Truck in F#
URL
leetcode.com/problems/maximum-units-on-a-tr..
Code
github.com/syohex/dotnet-study/blob/master/..
let maximumUnits (boxTypes: (int * int) list) (truckSize: int) : int =
let rec maximumUnits' boxTypes truckSize acc =
if truckSize = 0 then
acc
else
match boxTypes with
| [] -> acc
| (num, units) :: t ->
let count = System.Math.Min(truckSize, num)
maximumUnits' t (truckSize - count) (acc + (units * count))
let boxTypes' =
boxTypes
|> List.sortWith (fun (_, unit1) (_, unit2) -> compare unit2 unit1)
maximumUnits' boxTypes' truckSize 0