Shohei Yoshida
Shohei Yoshida's Blog

Follow

Shohei Yoshida's Blog

Follow

LeetCode 2225. Find Players With Zero or One Losses in F#

Shohei Yoshida's photo
Shohei Yoshida
·Nov 28, 2022·

1 min read

URL

leetcode.com/problems/find-players-with-zer..

Code

github.com/syohex/dotnet-study/blob/master/..

let findWinners (matches: (int * int) list) : int list * int list =
    let losers =
        matches
        |> List.fold
            (fun acc (_, loser) ->
                match Map.tryFind loser acc with
                | None -> Map.add loser 1 acc
                | Some (v) -> Map.add loser (v + 1) acc)
            Map.empty

    let winners =
        matches
        |> List.fold
            (fun acc (winner, _) ->
                match Map.tryFind winner acc with
                | None -> Map.add winner 1 acc
                | Some (v) -> Map.add winner (v + 1) acc)
            Map.empty
        |> Map.filter (fun k _ -> Map.containsKey k losers |> not)
        |> Map.keys
        |> Seq.toList
        |> List.sort

    let losers' =
        losers
        |> Map.filter (fun _ v -> v = 1)
        |> Map.keys
        |> Seq.toList
        |> List.sort

    winners, losers'
 
Share this