aboutsummaryrefslogtreecommitdiff
path: root/k_inverse_pairs.cpp
blob: 5601d19c994cf9def909f084edae72fec95c111d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "leetcode.h"

class Solution {
public:
  int kInversePairs(int n, int k) {
    vector<int> dp(k + 1, 0);
    int mod = 1e9 + 7;
    for (int i = 1; i <= n; i++) {
      vector<int> tmp(k + 1, 0);
      tmp[0] = 1;
      for (int j = 1; j <= k; j++) {
        long long val = (dp[j] + mod - ((j - i) >= 0 ? dp[j - i] : 0)) % mod;
        tmp[j] = (tmp[j - 1] + val) % mod;
      }
      dp = tmp;
    }
    return (dp[k] + mod - (k > 0 ? dp[k - 1] : 0)) % mod;
  }
};

int main() {
  Solution obj;
  cout << obj.kInversePairs(3, 0);
}