LeetCode 2336. Smallest Number in Infinite Set in F#
URL
Smallest Number in Infinite Set - LeetCode
Code
https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/problems/2336/main.fsx
type SmallestInfiniteSet =
{ Popped: Set<int> }
static member init() = { Popped = Set.empty }
static member popSmallest(s: SmallestInfiniteSet) : SmallestInfiniteSet * int =
let rec popSmallest' i popped =
if Set.contains i popped then
popSmallest' (i + 1) popped
else
{ Popped = (Set.add i popped) }, i
popSmallest' 1 s.Popped
static member addBack (num: int) (s: SmallestInfiniteSet) : SmallestInfiniteSet =
let popped = s.Popped
if Set.contains num popped then
{ Popped = (Set.remove num popped) }
else
s