Shohei Yoshida
Shohei Yoshida's Blog

Shohei Yoshida's Blog

LeetCode 377. Combination Sum IV in F#

Shohei Yoshida's photo
Shohei Yoshida
·Aug 5, 2022·

1 min read

URL

leetcode.com/problems/combination-sum-iv

Code

github.com/syohex/dotnet-study/blob/master/..

let combinationSum4 (nums: int list) (target: int) : int =
    let rec combinationSum4' i nums target (dp: int []) =
        if i > target then
            dp.[target]
        else
            nums
            |> List.iter (fun num ->
                if i - num >= 0 then
                    dp.[i] <- dp.[i] + dp.[i - num])

            combinationSum4' (i + 1) nums target dp

    let dp = Array.zeroCreate (target + 1)
    dp.[0] <- 1
    combinationSum4' 1 nums target dp
 
Share this