LeetCode 3264. Final Array State After K Multiplication Operations I in F#

URL

https://leetcode.com/problems/final-array-state-after-k-multiplication-operations-i/description/?envType=daily-question&envId=2024-12-16

Code

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

#r "nuget:FSharpx.Collections"

open FSharpx.Collections

let getFinalState (nums: int list) (k: int) (multiplier: int) : int list =
    let len = List.length nums

    let q =
        nums
        |> List.indexed
        |> List.fold (fun acc (i, n) -> PriorityQueue.insert (n, i) acc) (PriorityQueue.empty false)

    let q =
        seq { 1..k }
        |> Seq.fold
            (fun acc _ ->
                let (n, i), q = PriorityQueue.pop acc
                PriorityQueue.insert (n * multiplier, i) q)
            q

    seq { 1..len }
    |> Seq.fold
        (fun (acc, q) _ ->
            let (n, i), q = PriorityQueue.pop q
            (i, n) :: acc, q)
        ([], q)
    |> fst
    |> List.sortBy fst
    |> List.map snd