LeetCode 665. Non-decreasing Array in F#
URL
leetcode.com/problems/non-decreasing-array
Code
github.com/syohex/dotnet-study/blob/master/..
let checkPossibility (nums: int list) : bool =
let rec checkPossibility' nums prev2 prev1 count =
if count >= 2 then
false
else
match nums with
| [] -> true
| h :: t ->
if h >= prev1 then
checkPossibility' t prev1 h count
else if prev2 > h then
checkPossibility' t prev1 prev1 (count + 1)
else
checkPossibility' t h h (count + 1)
match nums with
| []
| _ :: []
| _ :: _ :: [] -> true
| h1 :: h2 :: rest ->
if h1 > h2 then
checkPossibility' rest h2 h2 1
else
checkPossibility' rest h1 h2 0