forked from prateekshyap/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RankTransformOfAnArray.java
54 lines (46 loc) · 1.5 KB
/
RankTransformOfAnArray.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
/*https://leetcode.com/problems/rank-transform-of-an-array/*/
class Solution {
public int[] arrayRankTransform(int[] arr) {
int i, j, n = arr.length;
int[] result = new int[n];
HashMap<Integer,ArrayList<Integer>> indices = new HashMap<Integer,ArrayList<Integer>>();
for (i = 0; i < n; ++i)
{
if (!indices.containsKey(arr[i])) indices.put(arr[i],new ArrayList<Integer>());
indices.get(arr[i]).add(i);
}
PriorityQueue<Integer> heap = new PriorityQueue<Integer>();
indices.forEach((k, v) -> { heap.add(k); });
j = 1;
while (!heap.isEmpty())
{
i = (Integer)heap.poll();
for (Integer index : indices.get(i))
result[index] = j;
++j;
}
return result;
}
}
class Solution {
public int[] arrayRankTransform(int[] arr) {
if(arr.length == 0) return arr;
int[] copy = Arrays.copyOf(arr,arr.length);
Arrays.sort(copy);
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
int last = copy[0], curr = 1;
map.put(last,curr);
for (int i = 1; i < copy.length; ++i)
{
if (copy[i] != last)
{
last = copy[i];
++curr;
map.put(last, curr);
}
}
for (int i = 0; i < arr.length; ++i)
arr[i] = map.get(arr[i]);
return arr;
}
}