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:

  1. The integer n is in the range [1, 1000] and k is in the range [0, 1000].

这道题给了我们1到n总共n个数字,让我们任意排列数组的顺序,使其刚好存在k个翻转对,所谓的翻转对,就是位置在前面的数字值大,而且题目中表明了结果会很大很大,要我们对一个很大的数字取余。对于这种结果巨大的题目,劝君放弃暴力破解或者是无脑递归,想都不用想,那么最先应该考虑的就是DP的解法了。我们需要一个二维的DP数组,其中dp[i][j]表示1到i的数字中有j个翻转对的排列总数,那么我们要求的就是dp[n][k]了,即1到n的数字中有k个翻转对的排列总数。现在难点就是要求递推公式了。我们想如果我们已经知道dp[n][k]了,怎么求dp[n+1][k],先来看dp[n+1][k]的含义,是1到n+1点数字中有k个翻转对的个数,那么实际上在1到n的数字中的某个位置加上了n+1这个数,为了简单起见,我们先让n=4,那么实际上相当于要在某个位置加上5,那么加5的位置就有如下几种情况:

xxxx5

xxx5x

xx5xx

x5xxx

5xxxx

这里xxxx表示1到4的任意排列,那么第一种情况xxxx5不会增加任何新的翻转对,因为xxxx中没有比5大的数字,而 xxx5x会新增加1个翻转对,xx5xx,x5xxx,5xxxx分别会增加2,3,4个翻转对。那么xxxx5就相当于dp[n][k],即dp[4][k],那么依次往前类推,就是dp[n][k-1], dp[n][k-2]...dp[n][k-n],这样我们就可以得出dp[n+1][k]的求法了:

dp[n+1][k] = dp[n][k] + dp[n][k-1] + ... + dp[n][k - n]

那么dp[n][k]的求法也就一目了然了:

dp[n][k] = dp[n - 1][k] + dp[n - 1][k-1] + ... + dp[n - 1][k - n + 1]

那么我们就可以写出代码如下了:

解法一:

class Solution {
public:
int kInversePairs(int n, int k) {
int M = ;
vector<vector<int>> dp(n + , vector<int>(k + , ));
dp[][] = ;
for (int i = ; i <= n; ++i) {
for (int j = ; j < i; ++j) {
for (int m = ; m <= k; ++m) {
if (m - j >= && m - j <= k) {
dp[i][m] = (dp[i][m] + dp[i - ][m - j]) % M;
}
}
}
}
return dp[n][k];
}
};

我们可以对上面的解法进行时间上的优化,还是来看我们的递推公式:

dp[n][k] = dp[n - 1][k] + dp[n - 1][k-1] + ... + dp[n - 1][k - n + 1]

我们可以用k+1代替k,得到:

dp[n][k+1] = dp[n - 1][k+1] + dp[n - 1][k] + ... + dp[n - 1][k + 1 - n + 1]

用第二个等式减去第一个等式可以得到:

dp[n][k+1] = dp[n][k] + dp[n - 1][k+1] - dp[n - 1][k - n + 1]

将k+1换回成k,可以得到:

dp[n][k] = dp[n][k-1] + dp[n - 1][k] - dp[n - 1][k - n]

我们可以发现当k>=n的时候,最后一项的数组坐标才能为非负数,从而最后一项才有值,所以我们再更新的时候只需要判断一下k和n的关系,如果k>=n的话,就要减去最后一项,这种递推式算起来更高效,减少了一个循环,参见代码如下:

解法二:

class Solution {
public:
int kInversePairs(int n, int k) {
int M = ;
vector<vector<int>> dp(n + , vector<int>(k + , ));
dp[][] = ;
for (int i = ; i <= n; ++i) {
dp[i][] = ;
for (int j = ; j <= k; ++j) {
dp[i][j] = (dp[i - ][j] + dp[i][j - ]) % M;
if (j >= i) {
dp[i][j] = (dp[i][j] - dp[i - ][j - i] + M) % M;
}
}
}
return dp[n][k];
}
};

参考资料:

https://discuss.leetcode.com/topic/93815/java-dp-o-nk-solution/2

https://discuss.leetcode.com/topic/93765/shared-my-c-o-n-k-solution-with-explanation

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] K Inverse Pairs Array K个翻转对数组的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 【leetcode dp】629. K Inverse Pairs Array

    https://leetcode.com/problems/k-inverse-pairs-array/description/ [题意] 给定n和k,求正好有k个逆序对的长度为n的序列有多少个,0& ...

  4. [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 ...

  5. LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  6. [LeetCode] 360. Sort Transformed Array 排序转换后的数组

    Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...

  7. LeetCode 088 Merge Sorted Array 合并两个有序数组

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:You ...

  8. 【LeetCode】Merge Sorted Array(合并两个有序数组)

    这道题是LeetCode里的第88道题. 题目描述: 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nu ...

  9. [Swift]LeetCode373. 查找和最小的K对数字 | Find K Pairs with Smallest Sums

    You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...

随机推荐

  1. 文献管理工具的使用(Mendeley和Endnote)

    以前是Mendeley的忠诚用户,但是最近在linux平台下出现的bug使我非常崩溃,之前辛辛苦苦整理的文献和分组,分分钟灰飞烟灭.所以希望能转用一个至少比较稳定的文献管理软件.刚好手头上有Mac版的 ...

  2. nvm配置

    多版本安装方式 卸载已有的Node.js 下载nvm 在C盘创建目录dev 在dev目中中创建两个子目录nvm和nodejs 并且把nvm包解压进去nvm目录中 在install.cmd文件上面右键选 ...

  3. 基于ECharts的饼状数据展示

    一.导入ECharts文件 二.HTML代码 大小后期自己调 三.后台代码 四.js代码 不要问为什么- -我是扒下来的 可复制代码: //基于准备好的dom,初始化echarts实例 var myD ...

  4. Visual Studio 2017 Key 激活码

    Visual Studio 2017(VS2017) 企业版 Enterprise 注册码:NJVYC-BMHX2-G77MM-4XJMR-6Q8QF Visual Studio 2017(VS201 ...

  5. LeetCode-391. 完美矩形(使用C语言编译,详解)

    链接:https://leetcode-cn.com/problems/perfect-rectangle/description/ 题目 我们有 N 个与坐标轴对齐的矩形, 其中 N > 0, ...

  6. Beta冲刺置顶随笔

    项目名称:城市安全风险管控系统 小组成员: 张梨贤.林静.周静平.黄腾飞 Beta冲刺随笔 Beta预备 Beta冲刺Day1 Beta冲刺Day2 Beta冲刺Day3 Beta冲刺Day4 Bet ...

  7. Alpha冲刺第一天

    Alpha冲刺第一天 站立式会议 项目进展 项目的第一天,主要工作是对项目的开发进行规划,以及将规划的成果转化为燃尽图与博客文章.依据项目需求分析报告与开题报告中已经完成的设计任务和项目规划,我们将系 ...

  8. 学号:201621123032 《Java程序设计》第6周学习总结

    1:本周学习总结 1.1: 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行一个总结 2:书面作业 2.1: clone方法 2.1.1 ...

  9. 关于使用栈将一般运算式翻译为后缀表达式并实现三级运算的方法及实例(cpp版)

    #include <iostream> #include <stack> #include <vector> #include <string> #de ...

  10. continue和break的特殊用法。

    break在程序中一般来说的作用就是跳出当前循环,然后再据需执行循环外的语句.continue也是对当前循环来说直接进入到下一次循环.其实我们在程序中有时候循环体嵌套太多,进行到某一步是希望直接bre ...