LeetCode 3097. Shortest Subarray With OR at Least K II in F#

URL

https://leetcode.com/problems/shortest-subarray-with-or-at-least-k-ii/description/?envType=daily-question&envId=2024-11-10

Code

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

open System

let setBits (n: int) (bits: int[]) =
    seq { 0..31 } |> Seq.iter (fun i -> bits.[i] <- bits.[i] + ((n >>> i) &&& 1))

let unsetBits (n: int) (bits: int[]) =
    seq { 0..31 } |> Seq.iter (fun i -> bits.[i] <- bits.[i] - ((n >>> i) &&& 1))

let orBits (bits: int[]) : int =
    bits
    |> Array.indexed
    |> Array.fold (fun acc (i, n) -> if n = 0 then acc else acc ||| (1 <<< i)) 0

let rec adjustWindowsSize left right (nums: int[]) (bits: int[]) acc k =
    if left > right || (orBits bits < k) then
        left, acc
    else
        let acc = min acc (right - left + 1)
        unsetBits nums.[left] bits
        adjustWindowsSize (left + 1) right nums bits acc k

let minimumArrayLength (nums: int list) (k: int) : int =
    let rec minimumArrayLength' left right (nums: int[]) (bits: int[]) acc =
        if right >= nums.Length then
            if acc = Int32.MaxValue then -1 else acc
        else
            setBits nums.[right] bits
            let left, acc = adjustWindowsSize left right nums bits acc k
            minimumArrayLength' left (right + 1) nums bits acc

    minimumArrayLength' 0 0 (List.toArray nums) (Array.zeroCreate 32) Int32.MaxValue