URL
https://leetcode.com/problems/daily-temperatures/description/?envType=daily-question&envId=2024-01-31
Code
https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/challenge/202401/daily_temperatures/main.fsx
let dailyTemperatures (temperatures: int[]) : int[] =
let rec popLowerTemperatures i stack (acc: int[]) =
match stack with
| [] -> acc, []
| h :: t ->
if temperatures.[h] < temperatures.[i] then
acc.[h] <- i - h
popLowerTemperatures i t acc
else
acc, stack
let rec dailyTemperatures' i stack acc =
if i >= Array.length temperatures then
acc
else
let acc', stack' = popLowerTemperatures i stack acc
dailyTemperatures' (i + 1) (i :: stack') acc'
let acc = Array.zeroCreate temperatures.Length
dailyTemperatures' 0 [] acc