Skip to content

Commit 7dab5db

Browse files
committed
Time: 15 ms (66.48%), Space: 17.5 MB (13.19%) - LeetHub
1 parent 791db53 commit 7dab5db

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int mod = 1e9+7;
4+
int kInversePairs(int n, int K) {
5+
//dp[i][j] denote till i numbers how many jth inversion pair-containing permutations are there
6+
vector<vector<int>> dp(n+1,vector<int>(K+1,0));
7+
dp[0][0]=0;
8+
for(int i=1;i<=K;i++){
9+
dp[1][i]=0;
10+
}
11+
for(int i=1;i<=n;i++){
12+
dp[i][0]=1;
13+
}
14+
for(int i=2;i<=n;i++){
15+
for(int j=1;j<=K;j++){
16+
dp[i][j]=(dp[i-1][j]+dp[i][j-1])%mod;
17+
if(j-i>=0){
18+
dp[i][j]=(dp[i][j]-dp[i-1][j-i]+mod)%mod;
19+
}
20+
}
21+
}
22+
return dp[n][K];
23+
}
24+
};

0 commit comments

Comments
 (0)