Shohei Yoshida
Shohei Yoshida's Blog

Shohei Yoshida's Blog

LeetCode 322. Coin Change in F#

Shohei Yoshida's photo
Shohei Yoshida
·May 21, 2022·

1 min read

URL

leetcode.com/problems/coin-change

Code

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

open System

let coinChange (coins: int list) (amount: int) : int =
    let dp = Array.init (amount + 1) (fun _ -> amount + 1)
    dp.[0] <- 0

    for i in 1..amount do
        for coin in coins do
            if i >= coin then
                dp.[i] <- Math.Min(dp.[i], dp.[i - coin] + 1)

    if dp.[amount] = amount + 1 then
        -1
    else
        dp.[amount]
 
Share this