-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path3109-find-the-index-of-permutation.js
More file actions
61 lines (50 loc) · 1.78 KB
/
3109-find-the-index-of-permutation.js
File metadata and controls
61 lines (50 loc) · 1.78 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* 3109. Find the Index of Permutation
* https://leetcode.com/problems/find-the-index-of-permutation/
* Difficulty: Medium
*
* 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 of the permutations of [1, 2, ..., n].
*
* Since the answer may be very large, return it modulo 109 + 7.
*/
/**
* @param {number[]} perm
* @return {number}
*/
var getPermutationIndex = function(perm) {
const smallerToRight = perm.map(() => 0);
mergeSort(0, perm.length - 1);
const MOD = 10 ** 9 + 7;
const factorials = computeFactorials(perm.length);
return perm.reduce((total, _, i) => {
return (total + smallerToRight[i] * factorials[perm.length - 1 - i]) % MOD;
}, 0);
function mergeSort(left, right) {
if (left >= right) return [left];
const mid = (left + right) >> 1;
const leftIndices = mergeSort(left, mid);
const rightIndices = mergeSort(mid + 1, right);
const merged = [];
let [leftPointer, rightPointer] = [0, 0];
while (leftPointer < leftIndices.length || rightPointer < rightIndices.length) {
if (rightPointer === rightIndices.length
|| (leftPointer < leftIndices.length
&& perm[leftIndices[leftPointer]] < perm[rightIndices[rightPointer]])) {
smallerToRight[leftIndices[leftPointer]] += rightPointer;
merged.push(leftIndices[leftPointer++]);
} else {
merged.push(rightIndices[rightPointer++]);
}
}
return merged;
}
function computeFactorials(n) {
const MOD = 10n ** 9n + 7n;
const factorials = [1n, 1n];
for (let i = 2n; i <= BigInt(n); i++) {
factorials.push(i * factorials[factorials.length - 1] % MOD);
}
return factorials.map(val => parseInt(val));
}
};