[Leetcode] DP--300. 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.

Solution:

This problem‘s dp solution is challenging to construct

1.

reference the tutorial of the solution; https://leetcode.com/articles/k-inverse-pairs-array/

very great explanation for 8 approaches, I only came up with the naive method and the recursive method

(1) Define the subproblem

dp[i][j] as the given integer i and the inverse pairs number j

dp[n][k] is the final answer

(2) Find the recursion

dp[i][j] =    dp[i-1][j-p]   for k = 0 to min(i-1, j)

(3) Get the base case

dp[0][j] = 0 for j = 0 to n

dp[i][0] = 1 for i = 0 to n

 1  M = 10**9 + 7
 2         dp = [[0]*(k+1) for i in range(0,n+1)]
 3
 4         #print ("dp: ", dp)
 5         for i in range(1, n+1):
 6             for j in range(0, k+1):
 7                 #print ("dp: ", i, j, dp[i][j])
 8                 if j == 0:
 9                    dp[i][j] = 1
10                    #print ("atttt: ", i, j, dp[i][0])
11
12                 else:
13                     for p in range(0, min(j, i-1)+1):
14                         dp[i][j] = (dp[i][j] + dp[i-1][j-p])%M
15                         #print ("daaaap: ", i, j, dp[i][0])
16
17         return dp[n][k]

It has TLE problem

2.

further to observe that if we use accumulated sum for dp[i][j], we can have

dp[i?1][j]+dp[i?1][j?i] if j?i≥0. Otherwise, we add all the elements of the previous row upto the current column j being considered.

 1  M = 10**9 + 7
 2         dp = [[0]*(k+1) for i in range(0,n+1)]
 3
 4         #print ("dp: ", dp)
 5         for i in range(1, n+1):
 6             for j in range(0, k+1):
 7                 #print ("dp: ", i, j, dp[i][j])
 8                 if j == 0:
 9                    dp[i][j] = 1
10                    #print ("atttt: ", i, j, dp[i][0])
11
12                 else:
13                     if j >= i:
14                         val = (dp[i-1][j] + M - dp[i-1][j-i]) % M
15                     else:
16                         val = (dp[i-1][j] + M - 0) % M
17                     dp[i][j] = (dp[i][j-1] + val)%M
18                         #print ("daaaap: ", i, j, dp[i][0])
19
20         if k > 0:
21             return (dp[n][k] + M - dp[n][k-1])%M
22         else:
23             return (dp[n][k] + M - 0)%M
时间: 2024-08-05 12:25:53

[Leetcode] DP--300. 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 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 a

[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 a

LeetCode | 0215. Kth Largest Element in an Array数组中的第K个最大元素【Python】

LeetCode 0215. Kth Largest Element in an Array数组中的第K个最大元素[Medium][Python][快排][堆] Problem LeetCode Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Example 1:

Leetcode 629.K个逆序对数组

K个逆序对数组 给出两个整数 n 和 k,找出所有包含从 1 到 n 的数字,且恰好拥有 k 个逆序对的不同的数组的个数. 逆序对的定义如下:对于数组的第i个和第 j个元素,如果满i < j且 a[i] > a[j],则其为一个逆序对:否则不是. 由于答案可能很大,只需要返回 答案 mod 109 + 7 的值. 示例 1: 输入: n = 3, k = 0 输出: 1 解释: 只有数组 [1,2,3] 包含了从1到3的整数并且正好拥有 0 个逆序对. 示例 2: 输入: n = 3, k =

LeetCode: Reverse Words in a String &amp;&amp; Rotate Array

Title: Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". https://leetcode.com/problems/reverse-words-in-a-string/ 思路:先将字符串全部逆转,在将每个单词逆转.具体实现,要注意空格,所以对字符串倒过来处理. cla

leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)

题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 说明: 1)设个标志可实现 实现: 1 class Solution { 2 public

leetcode 题解:Remove Duplicates from Sorted Array(已排序数组去重)

题目: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input array A

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

Leetcode 线性表 Swap Nodes in Pairs

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Total Submissions: 39302 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the