Участник:Hellhoundmipt/number-of-dice-rolls-with-target-sum/submissions/

Материал из DISCOPAL
Перейти к: навигация, поиск

https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/submissions/

Scala код

object Solution {
    val MOD = 1000000007
    def numRollsToTarget(d: Int, f: Int, target: Int): Int = {
        if (target < d || d * f < target) return 0
        var dp = Array.fill[Long](d+1, target+1)(0)
        dp(0)(0) = 1L
        for {
            i <- 1 to d
            j <- 1 to target
            k <- 1 to f
            if j >= k
        } {
            dp(i)(j) = (dp(i)(j) + dp(i - 1)(j - k)) % MOD
        }
        dp(d)(target).toInt
    }
}