949. Find the Index of Permutation
Given an array perm of length n, which is a permutation of [1, 2, ..., n], return the index of perm in the lexicographically sorted array of all permutations of [1, 2, ..., n].
Since the result can be very large, return it modulo 109 + 7.
Example 1:
Input: perm = [1,2,3]
Output: 0
Explanation:
The lexicographic order of all permutations:
[1,2,3], [1,3,2],[2,1,3],[2,3,1], [3,1,2],[3,2,1]
The permutation [1,2] appears at index 0.
Example 2:
Input: perm = [3,1,2]
Output: 4
Explanation:
The lexicographic order of all permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]
The permutation [3,1,2] appears at index 4.
Now Your Turn!
Pick the correct output for the given inputInput: perm = [4,2,3,1]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= n == perm.length <= 105
- perm is a valid permutation of [1, 2, ..., n].