Shohei Yoshida
Shohei Yoshida's Blog

Follow

Shohei Yoshida's Blog

Follow

LeetCode 45. Jump Game II in F#

Shohei Yoshida's photo
Shohei Yoshida
·Feb 8, 2023·

1 min read

URL

Jump Game II - LeetCode

Code

https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/problems/0045/main.fsx

open System

let jump (nums: int list) : int =
    let nums' = Array.ofList nums
    let len = nums'.Length
    let dp = Array.init len (fun _ -> len + 1)
    dp.[0] <- 0

    for i in 0 .. (len - 1) do
        for j in 1 .. nums'.[i] do
            if i + j < len then
                dp.[i + j] <- Math.Min(dp.[i + j], dp.[i] + 1)

    Array.last dp
 
Share this