URL
https://leetcode.com/problems/maximum-beauty-of-an-array-after-applying-operation/description/?envType=daily-question&envId=2024-12-11
Code
https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/challenge/202412/maximum_beauty_of_an_array_after_applying_operation/main.fsx
let maximumBeauty (nums: int[]) (k: int) : int =
let rec updateWindow left right (nums: int[]) =
if left > right then
left
else if nums.[right] - nums.[left] > 2 * k then
updateWindow (left + 1) right nums
else
left
let rec maximumBeauty' left right (nums: int[]) acc =
if right >= nums.Length then
acc
else
let left = updateWindow left right nums
maximumBeauty' left (right + 1) nums (max acc (right - left + 1))
maximumBeauty' 0 0 (Array.sort nums) 0