LeetCode 1574. Shortest Subarray to be Removed to Make Array Sorted in F#
URL
Shortest Subarray to be Removed to Make Array Sorted - LeetCode
Code
let findLengthOfShortestSubarray (arr: int[]) : int =
let rec rightPos i =
if i <= 0 then 0
else if arr.[i - 1] <= arr.[i] then rightPos (i - 1)
else i
let rec leftPos i =
if i >= arr.Length - 1 then arr.Length - 1
else if arr.[i] <= arr.[i + 1] then leftPos (i + 1)
else i
let rec findLengthOfShortestSubarray' i j left acc =
if i > left || j >= arr.Length then
acc
else if arr.[i] <= arr.[j] then
findLengthOfShortestSubarray' (i + 1) j left (min acc (j - i - 1))
else
findLengthOfShortestSubarray' i (j + 1) left acc
let left = leftPos 0
if left = arr.Length - 1 then
0
else
let right = rightPos (arr.Length - 1)
findLengthOfShortestSubarray' 0 right left (min (arr.Length - (left + 1)) right)