Skip to content

Practice Count the Number of Inversions

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3193
LeetCode Problem

Count the Number of Inversions

Problem Statement

Given n and requirements, count permutations of 0 through n - 1 satisfying every pair [end, count]: the prefix through index end must contain exactly count inversions. An inversion is a pair i < j whose values satisfy perm[i] > perm[j]. Return the count modulo 10^9 + 7.

Source: Count the Number of Inversions on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

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

# Example 2

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

# Example 3

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

Constraints

2 <= n <= 300
1 <= requirements.length <= n
requirements[i] = [endi, cnti]
0 <= endi <= n - 1
0 <= cnti <= 400
The input is generated such that there is at least one i such that endi == n - 1.
The input is generated such that all endi are unique.

Related LeetCode Problems

Practice more problems similar to Count the Number of Inversions.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.