forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount of Smaller Numbers After Self.js
77 lines (54 loc) · 2.14 KB
/
Count of Smaller Numbers After Self.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
73
74
75
76
77
var countSmaller = function(nums) {
if(nums===null || nums.length == 0) return []
let N = nums.length
const result = new Array(N).fill(0)
const numsWithIdx = new Array(N).fill(0)
for(let i = 0; i < nums.length; i++) {
const curr = nums[i]
numsWithIdx[i] = {val:curr, originalIdx: i}
}
divideAndConqueuer(0, nums.length - 1)
const resultList = []
result.forEach(el => resultList.push(el))
return resultList
function divideAndConqueuer(left, right) {
if(left < right) {
const mid = Math.floor(left + (right - left)/2)
divideAndConqueuer(left, mid)
divideAndConqueuer(mid+1, right)
merge(left, mid, right)
}
}
function merge(left, mid, right) {
const leftLen = mid - left + 1
const rightLen = right - mid
let rightSmallers = 0
const leftAux = new Array(leftLen).fill(0)
const rightAux = new Array(rightLen).fill(0)
for(let i = 0; i < leftLen; i++) {
leftAux[i] = numsWithIdx[left+i]
}
for(let i = 0; i < rightLen; i++) {
rightAux[i] = numsWithIdx[mid+1+i]
}
let leftPointer = 0
let rightPointer = 0
let insertAtPointer = left
while(leftPointer < leftLen && rightPointer < rightLen) {
if(leftAux[leftPointer].val <= rightAux[rightPointer].val) {
result[leftAux[leftPointer].originalIdx] += rightSmallers
numsWithIdx[insertAtPointer++] = leftAux[leftPointer++]
} else {
numsWithIdx[insertAtPointer++] = rightAux[rightPointer++]
rightSmallers++
}
}
while(rightPointer < rightLen) {
numsWithIdx[insertAtPointer++] = rightAux[rightPointer++]
}
while(leftPointer < leftLen) {
result[leftAux[leftPointer].originalIdx] += rightSmallers
numsWithIdx[insertAtPointer++] = leftAux[leftPointer++]
}
}
};