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 ...
随机推荐
- 后端程序员之路 34、Index搜索引擎实现分析3-对文章索引的两层分块
# part_indexer 对文章根据id的hash进行分块索引- 持有 search_index _inc_index[2]; search_index _history_index[2]; 进行 ...
- 死磕Spring之IoC篇 - Spring 应用上下文 ApplicationContext
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...
- Ubuntu pip版本的安装,卸载,查看,更新
pip版本的安装: sudo apt-get install python3-pip pip版本的查看: pip3 --version pip3 -V pip更新: sudo pip3 install ...
- pytest+jenkins+allure 生成测试报告发送邮件
前言第一部分:Pycharm for Gitee1. pycharm安装gitee插件2. gitee关联本地Git快速设置- 如果你知道该怎么操作,直接使用下面的地址简易的命令行入门教程:3. Gi ...
- 利用eigen库简单实现矩阵功能
eigen是目前运行速度较快的C++矩阵运算库,而且其轻便小巧安装方便的特点简直是吸引人啊!特做此笔记,记录一下这个安装简单.体积轻巧.功能强大的C++库. 1. Download and Insta ...
- TextRank算法及生产文本摘要方法介绍
TextRank 算法是一种用于文本的基于图的排序算法,其基本思想来源于谷歌的 PageRank算法,通过把文本分割成若干组成单元(句子),构建节点连接图,用句子之间的相似度作为边的权重,通过循环迭代 ...
- python3 多线程爬虫模板
原文:https://www.jianshu.com/p/06ae2373f560 1 import threading # 多线程模块 2 import queue # 队列模块 3 import ...
- node_exporter自定义监控
背景 我们在使用Zabbix的时候,可以自己写自定义脚本.在使用Promethues的时候,有很多的exporter,但是有一些特殊的情况没有,比如,我需要监控进程一启动就告警,但是进程没启动,是使用 ...
- BST(二叉搜索树)的基本操作
BST(二叉搜索树) 首先,我们定义树的数据结构如下: public class TreeNode { int val; TreeNode left; TreeNode right; public T ...
- AppDomain实现【插件式】开发
前言: 近期项目中需要实现"热插拔"式的插件程序,例如:定义一个插件接口:由不同开发人员实现具体的插件功能类库:并最终在应用中调用具体插件功能. 此时需要考虑:插件执行的安全性(隔 ...