[leetcode-629-K Inverse Pairs Array]
Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that there are exactly k inverse pairs.
We define an inverse pair as following: For ith and jth element in the array, if i < j and a[i] > a[j] then it's an inverse pair; Otherwise, it's not.
Since the answer may very large, the answer should be modulo 109 + 7.
Example 1:
Input: n = 3, k = 0
Output: 1
Explanation:
Only the array [1,2,3] which consists of numbers from 1 to 3 has exactly 0 inverse pair.
Example 2:
Input: n = 3, k = 1
Output: 2
Explanation:
The array [1,3,2] and [2,1,3] have exactly 1 inverse pair.
Note:
- The integer
nis in the range [1, 1000] andkis in the range [0, 1000].
思路:
这种求最优解的个数,而不用返回解本身的感觉好多都用动态规划,这题也不例外。
public class Solution {
int mo=;
public int kInversePairs(int n, int k) {
int[][] f=new int[][];
f[][]=;
for (int i=;i<=n;i++)
{
f[i][]=;
for (int j=;j<=k;j++)
{
f[i][j]=(f[i][j-]+f[i-][j])%mo;
if (j>=i) f[i][j]=(f[i][j]-f[i-][j-i]+mo)%mo;
}
}
return f[n][k];
}
}
参考自:
https://leetcode.com/kakahiguain/
[leetcode-629-K Inverse Pairs Array]的更多相关文章
- 629. K Inverse Pairs Array
Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...
- 【leetcode dp】629. K Inverse Pairs Array
https://leetcode.com/problems/k-inverse-pairs-array/description/ [题意] 给定n和k,求正好有k个逆序对的长度为n的序列有多少个,0& ...
- [LeetCode] K Inverse Pairs Array K个翻转对数组
Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...
- [Swift]LeetCode629. K个逆序对数组 | K Inverse Pairs Array
Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...
- Java实现 LeetCode 629 K个逆序对数组(动态规划+数学)
629. K个逆序对数组 给出两个整数 n 和 k,找出所有包含从 1 到 n 的数字,且恰好拥有 k 个逆序对的不同的数组的个数. 逆序对的定义如下:对于数组的第i个和第 j个元素,如果满i < ...
- Leetcode 629.K个逆序对数组
K个逆序对数组 给出两个整数 n 和 k,找出所有包含从 1 到 n 的数字,且恰好拥有 k 个逆序对的不同的数组的个数. 逆序对的定义如下:对于数组的第i个和第 j个元素,如果满i < j且 ...
- LeetCode:Search in Rotated Sorted Array I II
LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- LeetCode之“数组”:Rotate Array
题目链接 题目要求: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, ...
随机推荐
- Django入门笔记
Django入门笔记 **文档包含Django安装包.学习的笔记.代码等 安装 Django参考附件,只需要把附件拷贝到你需要的目录就行.Django是1.8.16版本 Python:在附件中,其中有 ...
- 关于XML(可扩展标记语言)的基础知识与写法------2017-05-18
XML(Extensible Markup Language) HTML:超文本标记语言,主要用来展示 XML:可扩展标记语言,用来做数据传输XML特点: 1.树状结构,有且只有一个根 2.标签名 ...
- PHP. 03 .ajax传输XML、 ajax传输json、封装
XML简介 XML 指可扩展标记语言 EXtensible Markup Language .射击的时候是用来船体数据的,虽然格式跟HTML类似 xml示例 <?xml version=&quo ...
- EntityFramework6.X 之LocalDB&ConnectionString
LocalDB 面向开发人员的SQL Server Express的执行模式,它的安装将复制启动SQL Server数据库引擎所需的最少文件集且使用特定连接字符串来启动连接,它是可以创建和打开SQL ...
- XSS研究1-来自外部的XSS攻击
引入: 上文中我们的例子是研究了来自内部的XSS攻击,通过输送一段有害js代码到受害者的机器,让其在受害者的域上运行这段有害JS代码来得到入侵目的.现在我们来看下来自外部的XSS攻击. 实践: 下面还 ...
- GRPC在NET上的实践(记录篇)
GRPC是什么? GRPC是一个开源RPC框架,于2015年3月开源,其由Google主要面向移动应用开发并基于HTTP/2协议标准而设计,基于Protobuf 3.0(Protocol Buffer ...
- python 基础篇 2
三.对变量.对象与赋值的浅析 1.不记住就完蛋了 1.1 记住:一切数据都是对象 1.2 记住:一切变量都是对数据对象的一个引用 1.3 分析:python内部的引用计数 sys.getrefcoun ...
- python爬虫从入门到放弃(五)之 正则的基本使用
什么是正则表达式 正则表达式是对字符串操作的一种逻辑公式,就是 事先定义好的一些特定字符.及这些特定字符的组合,组成一个"规则字符",这个"规则字符" 来表达对 ...
- js文件下载及命名(兼容多浏览器)
函数功能:实现主流浏览器的文件下载功能: 兼容性: >=IE10,Edge,chrome,firefox; 与后台的请求方式:GET请求, url携带参数 url?id=123(隐藏文件真实 ...
- C语言错题小本子
int a; ; a = ! x< //a的值是多少 我的答案:0, 正确答案:1 错误原因:没有熟练掌握运算符的优先级 // 找出下面无效的C语言变量名 A. _a B. main C. pr ...