[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
n
is in the range [1, 1000] andk
is 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, ...
随机推荐
- VS2013各版本激活密钥
Visual Studio Ultimate 2013 KEY(密钥):BWG7X-J98B3-W34RT-33B3R-JVYW9 Visual Studio Premium 2013 KEY(密钥) ...
- 关于JAVA自带MD5的方法
有空再详细解释 import java.security.MessageDigest; public class MD5 { public final static String MD51(Strin ...
- 【charger battery 充電 充電器 電池】停充的種類
Precondition : 配有 power path 功能的 BQ2589 手機. 接上 pc usb port. Origin : 今天有同事問我, 手機是否可以在接上 pc usb port ...
- java虚拟机学习-JVM调优总结(5)
数据类型 Java虚拟机中,数据类型可以分为两类:基本类型和引用类型.基本类型的变量保存原始值,即:他代表的值就是数值本身:而引用类型的变量保存引用值.“引用值”代表了某个对象的引用,而不是对象本身, ...
- mongoose populate
mongoose具备关系数据库一样的关联查询,通过在schema模型中设置ref属性,然后在查询时使用populate关键字,可以达到关联查询的目的. 以下内容参考了mongoose官方文档http: ...
- HttpClien Get&Post
新公司上班第二周,开始进军.Net Core,这方面的东西比较新,所以已经封装好的东西比较少,比如HttpClien之类的开源类库,找了NuGet好久,没有找到,所以先写个简陋的来用着先. 引用: u ...
- Python可视化:Seaborn库热力图使用进阶
前言 在日常工作中,经常可以见到各种各种精美的热力图,热力图的应用非常广泛,下面一起来学习下Python的Seaborn库中热力图(heatmap)如何来进行使用. 本次运行的环境为: windows ...
- VR全景:实体店与互联网的完美结合
VR元年已过,VR项目.VR创业潮转为理性,VR行业分为两个方向:硬件和内容.硬件又分为VR头显和辅助设备,内容又分为VR全景和VR虚拟内容,如游戏.娱乐.根据行业划分为VR+购物,VR+教育,VR+ ...
- 一天搞定HTML----a标签02
1.细说a标签 2.代码演示 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- Java文件流之练习
1 )将"今年是反法西斯胜利70周年,举国欢庆,所以要放假啦" 字符串 使用文件字符输出流 写入到oldhappy.txt文件中,复写10000行, 要求换行 在文件的开头写入当前 ...