LeetCode 2064. Minimized Maximum of Products Distributed to Any Store in F#
URL
Minimized Maximum of Products Distributed to Any Store - LeetCode
Code
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)