forked from maiquynhtruong/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count-of-smaller-numbers-after-self.java
54 lines (48 loc) · 1.71 KB
/
count-of-smaller-numbers-after-self.java
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
class Solution {
int[] smallerRight;
public List<Integer> countSmaller(int[] nums) {
int n = nums.length;
int[] indexes = new int[n];
smallerRight = new int[n];
for (int i = 0; i < n; i++) {
indexes[i] = i;
}
mergeSort(nums, indexes, 0, n-1);
List<Integer> res = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
res.add(smallerRight[i]);
}
return res;
}
public void mergeSort(int[] nums, int[] indexes, int start, int end) {
if (end <= start) return;
int mid = (end+start)/2;
mergeSort(nums, indexes, start, mid);
mergeSort(nums, indexes, mid+1, end);
merge(nums, indexes, start, end);
}
public void merge(int[] nums, int[] indexes, int start, int end) {
int[] sortArr = new int[end - start + 1];
int sortIndex = 0, rightCount = 0, mid = (start+end)/2, left = start, right = mid+1;
while (left <= mid && right <= end) {
if (nums[indexes[left]] > nums[indexes[right]]) {
rightCount++;
sortArr[sortIndex] = indexes[right++];
} else {
smallerRight[indexes[left]] += rightCount;
sortArr[sortIndex] = indexes[left++];
}
sortIndex++;
}
while (right <= end) {
sortArr[sortIndex++] = indexes[right++];
}
while (left <= mid) {
smallerRight[indexes[left]] += rightCount;
sortArr[sortIndex++] = indexes[left++];
}
for (int i = start; i <= end; i++) {
indexes[i] = sortArr[i-start];
}
}
}