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

题意:

  给出一个数组的长度和数组中最大元素的值,以及寻找最大元素的过程中的比较次数,问满足这样要求的数组有多少种?

思路:

  用动态规划来解决这道题,ways[i][j][k]表示数组长度为i,最大元素为j, 查找最大元素的过程中的比较次数。初始化的时候应该将ways[1][j][1] = 1;

  两个状态转换方程:

  1. ways[i][j][k] = j * ways[i-1][j][k]; //表示在最大值和最大比较次数不变的情况下,数组的长度增加1,状态的数量会增加为原来数量的j倍。这是因为[1 -- j]中的任何一个数字都可以和原来的状态组合而不改变最大值和查找次数。

  2. ways[i][j][k] += ∑(x = 1 to x = j - 1) ways[i-1][x][k-1]; // 表示在数组长度和查找次数都少1的情况下,当最大值比j要小时,都可以在数组中增加一个j,使其长度,最大值和查找的最大次数都改变,从而满足当前状态。

  最后将∑xways[n][x][k] 取余即可。

Code:

 1 class Solution {
2 long long ways[55][105][55];
3 const int mod = 1000000007;
4 public:
5 int numOfArrays(int n, int m, int num) {
6 memset(ways, 0, sizeof(ways));
7
8 for (int j = 1; j <= m; ++j) ways[1][j][1] = 1;
9 for (int i = 1; i <= n; ++i) {
10 for (int j = 1; j <= m; ++j) {
11 for (int k = 1; k <= num; ++k) {
12 long long s = 0;
13 s = (j * ways[i-1][j][k]) % mod;
14 for (int p = 1; p < j; ++p)
15 s = (s + ways[i-1][p][k-1]) % mod;
16 ways[i][j][k] = (s + ways[i][j][k]) % mod;
17 }
18 }
19 }
20 long long ans = 0;
21 for (int j = 1; j <= m; ++j) {
22 ans = (ans + ways[n][j][num]) % mod;
23 }
24 return ans;
25 }
26 };

参考:https://leetcode.com/problems/build-array-where-you-can-find-the-maximum-exactly-k-comparisons/discuss/586576/C%2B%2B-Bottom-Up-Dynamic-Programming-with-Explanation

1420. Build Array Where You Can Find The Maximum Exactly K Comparisons的更多相关文章

  1. Find the largest K numbers from array (找出数组中最大的K个值)

    Recently i was doing some study on algorithms. A classic problem is to find the K largest(smallest) ...

  2. 解决关于 npm build --prod ,出现 ERROR in budgets, maximum exceeded for initial. Budget 5 MB was exceeded by 750 kB的问题

    问题: 执行命令 :npm build --pord,出现以下错误: WARNING :. Ignoring. WARNING MB was exceeded by 3.73 MB. ERROR MB ...

  3. Data Structure Array: Given an array of of size n and a number k, find all elements that appear more than n/k times

    http://www.geeksforgeeks.org/given-an-array-of-of-size-n-finds-all-the-elements-that-appear-more-tha ...

  4. leetcode动态规划题目总结

    Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...

  5. Petya and Array (权值线段树+逆序对)

    Petya and Array http://codeforces.com/problemset/problem/1042/D time limit per test 2 seconds memory ...

  6. 【37.38%】【codeforces 722C】Destroying Array

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. array题目合集

    414. Third Maximum Number 给一个非空的整数数组,找到这个数组中第三大的值,如果不存在,那么返回最大的值.要求时间复杂度为o(n) 例如: Example 1: Input: ...

  8. Codeforces 722C. Destroying Array

    C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. 189. Rotate Array

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

随机推荐

  1. 后端程序员之路 4、一种monitor的做法

    record_t包含_sum._count._time_stamp._max._min最基础的一条记录,可以用来记录最大值.最小值.计数.总和metric_t含有RECORD_NUM(6)份recor ...

  2. linux 关闭对端口的监听

    netstat -anp | grep [端口号] [root@test-01 ~]# netstat -anp | grep 6665 tcp 0 0 0.0.0.0:6665 0.0.0.0:* ...

  3. 《C++ Primer》笔记 第7章 类

    成员函数的声明必须在类的内部,它的定义则既可以在类的内部也可以在类的外部.作为接口组成部分的非成员函数,它们的定义和声明都在类的外部. 定义在类内部的函数是隐式的inline函数. 成员函数通过一个名 ...

  4. Kafka集群消息积压问题及处理策略

    通常情况下,企业中会采取轮询或者随机的方式,通过Kafka的producer向Kafka集群生产数据,来尽可能保证Kafka分区之间的数据是均匀分布的. 在分区数据均匀分布的前提下,如果我们针对要处理 ...

  5. python+unittest+ddt数据驱动进行接口自动化测试

    所谓数据驱动测试,简单的理解为数据的改变从而驱动自动化测试的执行,最终引起测试结果的改变.通过使用数据驱动测试的方法,可以在需要验证多组数据测试场景中,使用外部数据源实现对输入输出与期望值的参数化,避 ...

  6. POJ-1847(SPFA+Vector和PriorityQueue优化的dijstra算法)

    Tram POJ-1847 这里其实没有必要使用SPFA算法,但是为了巩固知识,还是用了.也可以使用dijikstra算法. #include<iostream> #include< ...

  7. HDU_3746 Cyclic Nacklace 【KMP的应用】

    一.题目 HDU3746 二.分析 KMP比较好解决的一个问题:如果求一个串中的循环节? 仔细回想KMP的用法,重点是next数组,相当于就是后缀和前缀的比较,那么不正是方便了我们确定循环节? 如果以 ...

  8. git分支管理--rebase&merge详解

    目录 分支合并 git merge --squash [分支名] 注意点 git rebase [分支名] git rebase git rebase --abort git rebase -i gi ...

  9. Python中类的特殊属性和魔术方法

    1.属性 属性 含义 __name__ 类.函数.方法等的名字   __dir__ __module__ 类定义所在的模块名 __class__ 对象或类所属的类   只是返回基类 __bases__ ...

  10. protobuf基于java和javascript的使用

    目录 ProtoBuf介绍 整理下java和JavaScript的例子 demo测试 java作为服务端+客户端测试 客户端前端调用示例 项目地址 参考 ProtoBuf介绍 ProtoBuf 是go ...