DP: Coin Change

  • + 0 comments

    Simple Ruby DP solution:

    def make_change(n, coins)
      dp = Array.new(n + 1, 0)
      dp[0] = 1
      coins.each do |coin|
        (coin..n).each do |i|
          dp[i] += dp[i - coin]
        end
      end
      dp[n]
    end
    
    n, _m = gets.split.map(&:to_i)
    coins = gets.split.map(&:to_i)
    puts make_change(n, coins)