Skip to content

3193. Count the Number of Inversions 👍

  • Time: $O(400n^2) = O(n^2)$
  • Space: $O(400n) = O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
 public:
  int numberOfPermutations(int n, vector<vector<int>>& requirements) {
    constexpr int kMod = 1'000'000'007;
    constexpr int kMaxInversions = 400;
    // dp[i][j] := the number of ways to arrange the first i numbers of the
    // permutation s.t. there are j inversions
    vector<vector<int>> dp(n + 1, vector<int>(kMaxInversions + 1));
    vector<int> endToCnt(n + 1, -1);

    for (const vector<int>& requirement : requirements) {
      const int end = requirement[0];
      const int cnt = requirement[1];
      endToCnt[end + 1] = cnt;
    }

    // There's only one way to arrange a single number with zero inversions.
    dp[1][0] = 1;

    for (int i = 2; i <= n; ++i)
      for (int newInversions = 0; newInversions < i; ++newInversions)
        for (int j = 0; j + newInversions <= kMaxInversions; ++j) {
          const int inversionsAfterInsertion = j + newInversions;
          if (endToCnt[i] != -1 && inversionsAfterInsertion != endToCnt[i])
            continue;
          dp[i][inversionsAfterInsertion] += dp[i - 1][j];
          dp[i][inversionsAfterInsertion] %= kMod;
        }

    return dp[n][endToCnt[n]];
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
  public int numberOfPermutations(int n, int[][] requirements) {
    final int kMod = 1_000_000_007;
    final int kMaxInversions = 400;
    // dp[i][j] := the number of ways to arrange the first i numbers of the
    // permutation such that there are j inversions
    int[][] dp = new int[n + 1][kMaxInversions + 1];
    int[] endToCnt = new int[n + 1];
    Arrays.fill(endToCnt, -1);

    for (int[] requirement : requirements) {
      final int end = requirement[0];
      final int cnt = requirement[1];
      endToCnt[end + 1] = cnt;
    }

    // There's only one way to arrange a single number with zero inversions.
    dp[1][0] = 1;

    for (int i = 2; i <= n; ++i)
      for (int newInversions = 0; newInversions < i; ++newInversions)
        for (int j = 0; j + newInversions <= kMaxInversions; ++j) {
          final int inversionsAfterInsertion = j + newInversions;
          if (endToCnt[i] != -1 && inversionsAfterInsertion != endToCnt[i])
            continue;
          dp[i][inversionsAfterInsertion] += dp[i - 1][j];
          dp[i][inversionsAfterInsertion] %= kMod;
        }

    return dp[n][endToCnt[n]];
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
  def numberOfPermutations(self, n: int, requirements: list[list[int]]) -> int:
    kMod = 1_000_000_007
    kMaxInversions = 400
    # dp[i][j] := the number of ways to arrange the first i numbers of the
    # permutation s.t. there are j inversions
    dp = [[0] * (kMaxInversions + 1) for _ in range(n + 1)]
    endToCnt = {end + 1: cnt for end, cnt in requirements}

    # There's only one way to arrange a single number with zero inversions.
    dp[1][0] = 1

    for i in range(2, n + 1):
      for newInversions in range(i):
        for j in range(kMaxInversions - newInversions + 1):
          inversionsAfterInsertion = j + newInversions
          if i in endToCnt and inversionsAfterInsertion != endToCnt[i]:
            continue
          dp[i][inversionsAfterInsertion] += dp[i - 1][j]
          dp[i][inversionsAfterInsertion] %= kMod

    return dp[n][endToCnt[n]]