-
Notifications
You must be signed in to change notification settings - Fork 69
/
CountofSmallerNumbersAfterSelfSolution.java
50 lines (44 loc) Β· 1.38 KB
/
CountofSmallerNumbersAfterSelfSolution.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
class Solution {
Integer count[],indexes[];
void merge(int[] nums, int l, int m, int r) {
List<Pair<Integer,Integer>> list = new ArrayList<Pair<Integer,Integer>>();
int i=l,j=m+1,k=l;
while(i<=m && j<=r) {
if(nums[i]>nums[j]){
count[indexes[i]]+=r-j+1;
list.add(new Pair<>(nums[i],indexes[i++]));
}else
list.add(new Pair<>(nums[j],indexes[j++]));
}
while(i<=m)
list.add(new Pair<>(nums[i],indexes[i++]));
while(j<=r)
list.add(new Pair<>(nums[j],indexes[j++]));
for(Pair<Integer,Integer> pair : list){
nums[k]=pair.getKey();
indexes[k++]=pair.getValue();
}
}
void mergeSort(int[] nums, int l ,int r) {
if(l<r) {
int mid=(l+r)/2;
mergeSort(nums,l,mid);
mergeSort(nums,mid+1,r);
merge(nums,l,mid,r);
}
}
public List<Integer> countSmaller(int[] nums) {
count=new Integer[nums.length];
indexes=new Integer[nums.length];
for(int i=0;i<nums.length;i++){
count[i]=0;
indexes[i]=i;
}
mergeSort(nums,0,nums.length-1);
List<Integer> ans=new ArrayList<>();
for(int i=0;i<count.length;i++){
ans.add(count[i]);
}
return ans;
}
}