Skip to main content

LeetCode 2110. Number of Smooth Descent Periods of a Stock in F#

Published 1 min readMarkdown

URL

Number of Smooth Descent Periods of a Stock - LeetCode

Code

dotnet-study/fsharp/leetcode/challenge/202512/number_of_smooth_descent_periods_of_a_stock/main.fsx at master · syohex/dotnet-study

let getDescentPeriods (prices: int list) : int64 =
    let len = List.length prices

    let countCombinations left right =
        let count = right - left
        (1 + count) * count / 2

    let rec getDescentPeriods' prices prev left acc =
        match prices with
        | [] -> acc + (int64 <| countCombinations left len)
        | (i, h) :: t ->
            if h = prev - 1 then
                getDescentPeriods' t h left acc
            else
                let acc = acc + (int64 <| countCombinations left i)
                getDescentPeriods' t h i acc

    let prev = List.head prices
    let prices = prices |> List.indexed |> List.skip 1
    getDescentPeriods' prices prev 0 0L

No comments yet. Be the first to comment.

More from this blog