LeetCode 1352. Product of the Last K Numbers in F#

URL

Product of the Last K Numbers - LeetCode

Code

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

type ProductOfNumbers =
    { nums: ResizeArray<int> }

    static member empty() : ProductOfNumbers = { nums = ResizeArray [ 1 ] }

    static member add (num: int) (self: ProductOfNumbers) : ProductOfNumbers =
        if num = 0 then
            { nums = ResizeArray [ 1 ] }
        else
            self.nums.Add (num * self.nums.[self.nums.Count - 1])
            self

    static member getProduct (k: int) (self: ProductOfNumbers) : int =
        if k >= self.nums.Count then
            0
        else
            let len = self.nums.Count
            self.nums.[len - 1] / self.nums.[len - 1 - k]