LeetCode 2461. Maximum Sum of Distinct Subarrays With Length K in F#

URL

Maximum Sum of Distinct Subarrays With Length K - LeetCode

Code

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

let maximumSubarraySum (nums: int[]) (k: int) : int64 =
    let rec maximumSubarraySum' i (nums: int64[]) (sum: int64) m acc =
        if i >= nums.Length then
            acc
        else
            let sum = sum + nums.[i] - nums.[i - k]

            let m =
                match Map.tryFind nums.[i] m with
                | Some(v) -> Map.add nums.[i] (v + 1) m
                | None -> Map.add nums.[i] 1 m

            let count = Map.find nums.[i - k] m
            let m = if count = 1 then Map.remove nums.[i - k] m else m
            let acc = if Map.count m = k then max sum acc else acc
            maximumSubarraySum' (i + 1) nums sum m acc

    let nums = Array.map int64 nums

    let (m, sum) =
        nums
        |> Array.take k
        |> Array.fold
            (fun (acc, sum) n ->
                match Map.tryFind n acc with
                | Some(v) -> Map.add n (v + 1) acc, sum + n
                | None -> Map.add n 1 acc, sum + n)
            (Map.empty, 0L)

    let acc = if Map.count m = k then sum else 0
    maximumSubarraySum' k nums sum m acc