Count the Number of Inversions - Problem
Count the Number of Inversions
You're given an integer
What is an inversion?
In an array, an inversion is a pair of indices
Your Goal:
Find how many permutations of
Since the answer can be very large, return it modulo
Example: If
You're given an integer
n and a 2D array requirements, where each requirements[i] = [end_i, cnt_i] represents a constraint on the permutation.What is an inversion?
In an array, an inversion is a pair of indices
(i, j) where i < j but nums[i] > nums[j]. It's essentially when a larger element appears before a smaller one.Your Goal:
Find how many permutations of
[0, 1, 2, ..., n-1] satisfy ALL the given requirements. For each requirement [end_i, cnt_i], the subarray perm[0..end_i] must have exactly cnt_i inversions.Since the answer can be very large, return it modulo
109 + 7.Example: If
n=3 and requirements=[[2,2]], we need permutations where the first 3 elements have exactly 2 inversions. The permutation [2,1,0] works because it has inversions: (0,1), (0,2), (1,2) - but wait, that's 3 inversions, not 2! Input & Output
example_1.py โ Basic Case
$
Input:
n = 3, requirements = [[2,2]]
โบ
Output:
2
๐ก Note:
We need permutations of [0,1,2] where the first 3 elements have exactly 2 inversions. Valid permutations: [2,0,1] has inversions (0,1), (0,2) = 2 total. [1,2,0] has inversions (0,2), (1,2) = 2 total.
example_2.py โ Multiple Requirements
$
Input:
n = 3, requirements = [[0,0], [2,2]]
โบ
Output:
1
๐ก Note:
First element must create 0 inversions (so it's the minimum), and total must be 2. Only [0,2,1] works: 0 inversions at position 0, and inversions (1,2) gives 1 total... wait, this needs recalculation.
example_3.py โ Impossible Case
$
Input:
n = 2, requirements = [[0,1]]
โบ
Output:
0
๐ก Note:
With only 1 element at position 0, it's impossible to have 1 inversion since inversions need at least 2 elements.
Constraints
- 2 โค n โค 300
- 0 โค requirements.length โค n
- requirements[i] = [endi, cnti]
- 0 โค endi โค n - 1
- 0 โค cnti โค 400
- All endi are distinct
Visualization
Tap to expand
Understanding the Visualization
1
Start Empty
Begin with no one seated - 1 way to have 0 inversions
2
Seat First Person
Place person with skill 0, 1, or 2 in first seat - creates different inversion potentials
3
Add Second Person
Each new person can sit ahead of or behind existing people, creating specific numbers of inversions
4
Check Constraints
At required checkpoints, eliminate arrangements that don't meet inversion requirements
5
Continue Building
Keep adding people while tracking valid arrangements until all seats are filled
Key Takeaway
๐ฏ Key Insight: Build permutations incrementally using DP, where placing element k at position p with r smaller elements already placed contributes exactly (k-r) new inversions. This allows us to efficiently count valid arrangements without generating all permutations.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code