LeetCode 624. Maximum Distance in Arrays in F#

URL

Maximum Distance in Arrays - LeetCode

Code

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

let maxDistance (arrays: int list list) : int =
    let rec maxDistance' arrays minVal maxVal ret =
        match arrays with
        | [] -> ret
        | h :: t ->
            let front, back = List.head h, List.last h
            let ret = max ret (max (abs (maxVal - front)) (abs (back - minVal)))
            maxDistance' t (min minVal front) (max maxVal back) ret

    match arrays with
    | [] -> failwith "never reach here"
    | h :: t -> maxDistance' t (List.head h) (List.last h) 0