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. 缓存cache和缓冲区buffer

    一.cache 1.cache的定义.从宏观上讲,缓存是处理速度不匹配的问题.可以是静态缓存(内存缓存.磁盘缓存).动态缓存(前端的缓存)和数据库缓存.另一个角度,从CPU来看,可以是寄存器和内存之间 ...

  2. React 中的 onInput/onChange

    参考链接:https://stackoverflow.com/questions/38256332/in-react-whats-the-difference-between-onchange-and ...

  3. HDOJ-3001(TSP+三进制状态压缩)

    Traving HDOJ-3001 这题考察的是状态压缩dp和tsp问题的改编 需要和传统tsp问题区分的事,这题每个点最多可以经过两次故状态有3种:0,1,2 这里可以模仿tsp问题的二进制压缩方法 ...

  4. HDOJ-1540(线段树+较复杂的单点修改和区间查询)

    Tunnel Warfare HDOJ-1540 这题关于线段树的操作有一定的难度,需要较好的思维能力. 关于题目的详细解答已经在代码中体现了. #include<iostream> #i ...

  5. Java 中为什么要设计包装类

    尽人事,听天命.博主东南大学硕士在读,热爱健身和篮球,乐于分享技术相关的所见所得,关注公众号 @ 飞天小牛肉,第一时间获取文章更新,成长的路上我们一起进步 本文已收录于 「CS-Wiki」Gitee ...

  6. 使用 .NET CLI 构建项目脚手架

    前言 在微服务场景中,开发人员分配到不同的小组,系统会拆分为很多个微服务,有一点是,每个项目都需要单元测试,接口文档,WebAPI接口等,创建新项目这些都是重复的工作,而且还要保证各个项目结构的大体一 ...

  7. 报错NameError: name ‘null’ is not defined的解决方法

    报错NameError: name 'null' is not defined的解决方法 eval()介绍 eval()函数十分强大,官方demo解释为:将字符串str当成有效的表达式来求值并返回计算 ...

  8. java基础:变量、常量与作用域

    变量就是可以变化的量,每个变量都必须声明其类型,Java 变量是程序中最基本的存储单元,其要素包括变量名,变量类型和作用域.作用域 类变量 实例变量 局部变量常量初始化后不能在改变值,不会变动的值,它 ...

  9. vue 实现页面嵌套pdf之vue-pdf插件

    近期vue移动端项目中遇到了页面内,嵌套展示pdf内容.实现方法很多种,可以用iframe嵌套,但不利于引擎优化seo.所以在网上找到了vue-pdf这个插件,这个插件非常好用,其中封装的方法也很多, ...

  10. 解决图片把父元素向下撑大大约3px问题

    现象  bug: 图片在div\li\dt 等  图片把父元素向下撑大大约3px  <style>         img {             width: 30%; //这里由于 ...