forked from Adespinoza/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_044.js
72 lines (65 loc) Β· 1.82 KB
/
problem_044.js
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
62
63
64
65
66
67
68
69
70
71
72
/**
* Returns the number of inversions in nums
* @param {number[]} nums
* @return {number}
*/
function countInversions(nums) {
// Use merge sort to count inversions
const aux = Array(nums.length);
return mergeSort(nums, aux, 0, nums.length - 1);
}
/**
* Recursive split method of merge sort
* @param {number[]} nums
* @param {number[]} aux
* @param {number} low
* @param {number} high
* @return {number}
*/
function mergeSort(nums, aux, low, high) {
let inversions = 0;
if (low < high) {
const mid = Math.floor((low + high) / 2);
inversions = mergeSort(nums, aux, low, mid);
inversions += mergeSort(nums, aux, mid + 1, high);
inversions += merge(nums, aux, low, mid, high);
}
return inversions;
}
/**
* Merge process of merge sort, and returns inversion count
* @param {number[]} nums
* @param {number[]} aux
* @param {number} low
* @param {number} mid
* @param {number} high
* @return {number}
*/
function merge(nums, aux, low, mid, high) {
let inversions = 0;
// Copy both halves into aux
for (let i = low; i <= high; i++) {
aux[i] = nums[i];
}
let helperLeft = low;
let helperRight = mid + 1;
let current = low;
// Iterate through helper array. Compare left and right, copying back the smaller element of both halves
while (helperLeft <= mid && helperRight <= high) {
if (aux[helperLeft] <= aux[helperRight]) {
nums[current] = aux[helperLeft];
helperLeft++;
} else {
nums[current] = aux[helperRight];
helperRight++;
inversions += mid - helperLeft + 1; // Where we count the number of inversions
}
current++;
}
// copy the rest of the left side of the array into the target array
const remaining = mid - helperLeft;
for (let i = 0; i <= remaining; i++) {
nums[current + i] = aux[helperLeft + i];
}
return inversions;
}