Shohei Yoshida
Shohei Yoshida's Blog

Follow

Shohei Yoshida's Blog

Follow

LeetCode 2244. Minimum Rounds to Complete All Tasks in F#

Shohei Yoshida's photo
Shohei Yoshida
·Jan 4, 2023·

1 min read

URL

https://leetcode.com/problems/minimum-rounds-to-complete-all-tasks/description/

Code

https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/challenge/202301/minimum_rounds_to_complete_all_tasks/main.fsx

let minimumRounds (tasks: int list) : int =
    let rec minimumRounds' values acc =
        match values with
        | [] -> acc
        | h :: t ->
            if h = 1 then
                -1
            else
                minimumRounds'
                    t
                    (acc
                     + (System.Math.Ceiling((double h) / 3.0) |> int))

    let freq =
        tasks
        |> List.fold
            (fun acc n ->
                match Map.tryFind n acc with
                | Some (v) -> Map.add n (v + 1) acc
                | None -> Map.add n 1 acc)
            Map.empty

    minimumRounds' (Map.values freq |> Seq.toList) 0
 
Share this