LeetCode 787. Cheapest Flights Within K Stops in F#

URL

https://leetcode.com/problems/cheapest-flights-within-k-stops/description/?envType=daily-question&envId=2024-02-23

Code

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

let flightsToGraph flights =
    let rec flightsToGraph' flights acc =
        match flights with
        | [] -> acc
        | (src, dst, price) :: t ->
            match Map.tryFind src acc with
            | Some(v) -> flightsToGraph' t (Map.add src ((dst, price) :: v) acc)
            | None -> flightsToGraph' t (Map.add src [ (dst, price) ] acc)

    flightsToGraph' flights Map.empty

let findCheapestPrice (n: int) (flights: (int * int * int) list) (src: int) (dst: int) (k: int) : int =
    let rec findCheapestPrice' step q (minPrices: int[]) graph =
        match q with
        | [] ->
            if minPrices.[dst] = System.Int32.MaxValue then
                -1
            else
                minPrices.[dst]
        | _ ->
            if step > k then
                if minPrices.[dst] = System.Int32.MaxValue then
                    -1
                else
                    minPrices.[dst]
            else
                let q' = q |> List.filter (fun (city, price) -> price < minPrices.[city])
                q' |> List.iter (fun (city, price) -> minPrices.[city] <- price)

                let q'' =
                    q'
                    |> List.fold
                        (fun nextCities (city, total) ->
                            match Map.tryFind city graph with
                            | None -> nextCities
                            | Some(v) ->
                                v |> List.fold (fun acc (dst, price) -> (dst, total + price) :: acc) nextCities)
                        []

                findCheapestPrice' (step + 1) q'' minPrices graph

    let graph = flightsToGraph flights
    let minPrices = Array.init n (fun _ -> System.Int32.MaxValue)
    findCheapestPrice' -1 [ (src, 0) ] minPrices graph