-
Notifications
You must be signed in to change notification settings - Fork 2
/
315.count-of-smaller-numbers-after-self.java
79 lines (66 loc) · 2.18 KB
/
315.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* @lc app=leetcode id=315 lang=java
*
* [315] Count of Smaller Numbers After Self
*/
// @lc code=start
class Solution {
public List<Integer> countSmaller(int[] nums) {
int[] index = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
index[i] = i;
}
int[] result = new int[nums.length];
mergeSort(nums, index, 0, nums.length - 1, result);
//add result to list
List<Integer> resultList = new ArrayList<>();
for (int i : result) resultList.add(i);
return resultList;
}
private void mergeSort(int[] nums, int index[], int left, int right, int[] result) {
if (left >= right) {
return;
}
int mid = (left + right) / 2;
mergeSort(nums, index, left, mid, result);
mergeSort(nums, index, mid + 1, right, result);
int rightCount = 0;
int i = left;
for (int j = mid + 1; i <= mid; i++) {
while (j <= right && nums[index[i]] > nums[index[j]] ) {
rightCount++;
j++;
}
result[index[i]] += rightCount;
}
while (i <= mid) {
result[index[i]] += rightCount;
i++;
}
merge(nums, index, left, right);
}
public void merge(int[] nums, int[] index, int start, int end) {
// create a temp array
int temp[] = new int[end - start + 1];
// crawlers for both intervals and for temp
int mid = start + (end - start) / 2;
int i = start, j = mid + 1, k = 0;
// traverse both arrays and in each iteration add smaller of both elements in temp
while(i <= mid && j <= end) {
temp[k++] = nums[index[i]] <= nums[index[j]] ? index[i++] : index[j++];
}
// add elements left in the first interval
while(i <= mid) {
temp[k++] = index[i++];
}
// add elements left in the second interval
while(j <= end) {
temp[k++] = index[j++];
}
// copy temp to original interval
for(i = start; i <= end; i += 1) {
index[i] = temp[i - start];
}
}
}
// @lc code=end