| 1 | //Relative Sorting |
| 2 | class Solution { |
| 3 | public int[] arrayRankTransform(int[] arr) { |
| 4 | int n = arr.length; |
| 5 | ArrayList<Integer> indexArr = new ArrayList<>(); |
| 6 | for(int i=0;i<n;i++){ |
| 7 | indexArr.add(i); |
| 8 | } |
| 9 | Collections.sort(indexArr, new Comparator<Integer>(){ |
| 10 | public int compare(Integer x, Integer y){ |
| 11 | return arr[x] - arr[y]; //cust comp, inc sort |
| 12 | } |
| 13 | }); |
| 14 | int rank=0; |
| 15 | int prev=Integer.MAX_VALUE; |
| 16 | int res[] = new int[n]; |
| 17 | for(int index : indexArr){ |
| 18 | if(prev!=arr[index]){ |
| 19 | prev = arr[index]; |
| 20 | rank++; |
| 21 | } |
| 22 | res[index] = rank; |
| 23 | } |
| 24 | return res; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | //Priority Queue |