LeetCode-in-Java

3193. Count the Number of Inversions

Hard

You are given an integer n and a 2D array requirements, where requirements[i] = [endi, cnti] represents the end index and the inversion count of each requirement.

A pair of indices (i, j) from an integer array nums is called an inversion if:

Return the number of permutations perm of [0, 1, 2, ..., n - 1] such that for all requirements[i], perm[0..endi] has exactly cnti inversions.

Since the answer may be very large, return it modulo 109 + 7.

Example 1:

Input: n = 3, requirements = [[2,2],[0,0]]

Output: 2

Explanation:

The two permutations are:

Example 2:

Input: n = 3, requirements = [[2,2],[1,1],[0,0]]

Output: 1

Explanation:

The only satisfying permutation is [2, 0, 1]:

Example 3:

Input: n = 2, requirements = [[0,0],[1,0]]

Output: 1

Explanation:

The only satisfying permutation is [0, 1]:

Constraints:

Solution

import java.util.Arrays;

public class Solution {
    private static final int MOD = 1_000_000_007;

    public int numberOfPermutations(int n, int[][] r) {
        Arrays.sort(r, (o1, o2) -> o1[0] - o2[0]);
        if (r[0][0] == 0 && r[0][1] > 0) {
            return 0;
        }
        int ri = r[0][0] == 0 ? 1 : 0;
        long a = 1;
        long t;
        int[][] m = new int[n][401];
        m[0][0] = 1;
        for (int i = 1; i < m.length; i++) {
            m[i][0] = m[i - 1][0];
            for (int j = 1; j <= i; j++) {
                m[i][j] = (m[i][j] + m[i][j - 1]) % MOD;
                m[i][j] = (m[i][j] + m[i - 1][j]) % MOD;
            }
            for (int j = i + 1; j <= r[ri][1]; j++) {
                m[i][j] = (m[i][j] + m[i][j - 1]) % MOD;
                m[i][j] = (m[i][j] + m[i - 1][j]) % MOD;
                m[i][j] = (m[i][j] - m[i - 1][j - i - 1]);
                if (m[i][j] < 0) {
                    m[i][j] += MOD;
                }
            }
            if (r[ri][0] == i) {
                t = m[i][r[ri][1]];
                if (t == 0) {
                    return 0;
                }
                Arrays.fill(m[i], 0);
                m[i][r[ri][1]] = 1;
                a = (a * t) % MOD;
                ri++;
            }
        }
        return (int) a;
    }
}