1420. Build Array Where You Can Find The Maximum Exactly K Comparisons
Given three integers n
, m
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 exactlyn
integers.1 <= arr[i] <= m
where(0 <= i < n)
.- After applying the mentioned algorithm to
arr
, the valuesearch_cost
is equal tok
.
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 };
1420. Build Array Where You Can Find The Maximum Exactly K Comparisons的更多相关文章
- 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) ...
- 解决关于 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 ...
- 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 ...
- leetcode动态规划题目总结
Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...
- Petya and Array (权值线段树+逆序对)
Petya and Array http://codeforces.com/problemset/problem/1042/D time limit per test 2 seconds memory ...
- 【37.38%】【codeforces 722C】Destroying Array
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- array题目合集
414. Third Maximum Number 给一个非空的整数数组,找到这个数组中第三大的值,如果不存在,那么返回最大的值.要求时间复杂度为o(n) 例如: Example 1: Input: ...
- Codeforces 722C. Destroying Array
C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 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 ...
随机推荐
- Autocad中使用命令来调用python对Autocad二次开发打包后的exe程序
在Autocad中直接调用Python二次开发程序是有必要的,下面介绍一种方法来实现这个功能: 其基本思路是:先将二次开发的程序打包为可执行程序exe,然后编写lsp文件,该文件写入调用exe程序的语 ...
- javascript中的模块系统
目录 简介 CommonJS和Nodejs AMD异步模块加载 CMD ES modules和现代浏览器 在HTML中使用module和要注意的问题 简介 在很久以前,js只是简单的作为浏览器的交互操 ...
- gRPC-go源码(2):ClientConn
摘要 在上一篇文章中,我们聊了聊gRPC是怎么管理一条从Client到Server的连接的. 我们聊到了gRPC拥有Resolver,用来解析地址:拥有Balancer,用来做负载均衡. 在这一篇文章 ...
- 大话Spark(6)-源码之SparkContext原理剖析
SparkContext是整个spark程序通往集群的唯一通道,他是程序的起点,也是程序的终点. 我们的每一个spark个程序都需要先创建SparkContext,接着调用SparkContext的方 ...
- 178. 分数排名 + MySql + RANK() OVER
178. 分数排名 LeetCode_MySql_178 题目描述 题解分析 排名函数 DENSE_RANK().如果使用 DENSE_RANK() 进行排名会得到:1,1,2,3,4. RANK() ...
- 1.2 Python3基础-规范
>>返回主目录 总的来说,如果安装的不是安装的Anaconda,pip命令还是经常会用到的(cmd模式使用),当然也可以在PyCharm中直接安装 PEP8规范,我另有一篇博客已经写好,可 ...
- java 方法详解
什么是方法 方法的定义和调用 值传递与引用传递 值传递:指的是在方法调用时,传递的是参数是按值的拷贝传递. 特点:传递的是值的拷贝,也就是传递后就互不相关了. 引用传递:指的是在方法调用时,传递的参数 ...
- 漏洞复现-CVE-2017-12629-RCE-solr:XXE
0x00 实验环境 攻击机:Win 10.Win Server 2012 R2 靶机:Ubuntu18 (docker搭建的vulhub靶场) 0x01 影响版本 Apac ...
- springboot整合mybatis。mapper.xml资源文件放置到resources文件夹下的配置&别名使用配置
- ssh 免登录配置
引子 近日海淘了一个 mini-PC:Gigabyte GB-BSRE-1605,此设备虽采用 amd 嵌入式低功耗处理器,性能相比现在自己所用的设备却有不小提升,加上先前升级电脑多余一些 ssd 和 ...