MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / arrayRankTransform

Method arrayRankTransform

RankTransformofanArray.java:3–25  ·  view source on GitHub ↗
(int[] arr)

Source from the content-addressed store, hash-verified

1//Relative Sorting
2class 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

Callers

nothing calls this directly

Calls 3

sortMethod · 0.80
addMethod · 0.45
isEmptyMethod · 0.45

Tested by

no test coverage detected