LeetCode 2134. Minimum Swaps to Group All 1's Together II in F#

URL

Minimum Swaps to Group All 1's Together II - LeetCode

Code

https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/challenge/202408/minimum_swaps_to_group_all_1s_together2/main.fsx

let minSwaps (nums: int list) : int =
    let minSwaps' nums v =
        let countNum = List.filter ((=) v) >> List.length

        let total = countNum nums
        let maxValInWindow = nums |> List.windowed total |> List.map countNum |> List.max
        total - maxValInWindow

    min (minSwaps' nums 0) (minSwaps' nums 1)