LeetCode 2064. Minimized Maximum of Products Distributed to Any Store in F#

URL

Minimized Maximum of Products Distributed to Any Store - LeetCode

Code

https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/challenge/202411/minimized_maximum_of_products_distributed_to_any_store/main.fsx

let minimizedMaximum (n: int) (quantities: int list) : int =
    let rec canDistribute count i n products quantities =
        if i >= n then
            false
        else if products <= count then
            match quantities with
            | [] -> true
            | h :: t -> canDistribute count (i + 1) n h t
        else
            canDistribute count (i + 1) n (products - count) quantities

    let rec minimizedMaximum' left right =
        if left >= right then
            left
        else
            let mid = left + (right - left) / 2

            if canDistribute mid 0 n (List.head quantities) (List.tail quantities) then
                minimizedMaximum' left mid
            else
                minimizedMaximum' (mid + 1) right

    minimizedMaximum' 1 (List.max quantities)