1420. Build Array Where You Can Find The Maximum Exactly K Comparisons

87 篇文章 0 订阅

Given three integers nm and k. Consider the following algorithm to find the maximum element of an array of positive integers:

 

You should build the array arr which has the following properties:

  • arr has exactly n integers.
  • 1 <= arr[i] <= m where (0 <= i < n).
  • After applying the mentioned algorithm to arr, the value search_cost is equal to k.

Return the number of ways to build the array arr under the mentioned conditions. As the answer may grow large, the answer must be computed modulo 10^9 + 7.

 

Example 1:

Input: n = 2, m = 3, k = 1
Output: 6
Explanation: The possible arrays are [1, 1], [2, 1], [2, 2], [3, 1], [3, 2] [3, 3]

Example 2:

Input: n = 5, m = 2, k = 3
Output: 0
Explanation: There are no possible arrays that satisify the mentioned conditions.

Example 3:

Input: n = 9, m = 1, k = 1
Output: 1
Explanation: The only possible array is [1, 1, 1, 1, 1, 1, 1, 1, 1]

Example 4:

Input: n = 50, m = 100, k = 25
Output: 34549172
Explanation: Don't forget to compute the answer modulo 1000000007

Example 5:

Input: n = 37, m = 17, k = 7
Output: 418930126

 

Constraints:

  • 1 <= n <= 50
  • 1 <= m <= 100
  • 0 <= k <= n

思路:DP,dp[i][j][k]表示用了i个数,当前最大值为j,用了k个cost

class Solution(object):
    def numOfArrays(self, n, m, k):
        """
        :type n: int
        :type m: int
        :type k: intå
        :rtype: int
        """
        mod = 10**9+7
        dp = [[[0 for _ in range(k+1)] for _ in range(m)] for _ in range(n)]
        for j in range(m):
            dp[0][j][1] = 1

        for i in range(1,n):
            for j in range(m):
                for k in range(1,k+1):
                    dp[i][j][k] = dp[i-1][j][k] * (j+1)
                    for jj in range(j):
                        dp[i][j][k] += dp[i-1][jj][k-1]
                    dp[i][j][k] %= mod

        return sum(dp[n-1][idx][k] for idx in range(m)) % mod

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值