-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC1331.java
More file actions
59 lines (43 loc) · 1.34 KB
/
LC1331.java
File metadata and controls
59 lines (43 loc) · 1.34 KB
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
/*
* LC1331
*/
import java.util.*;
import java.util.stream.Collectors;
public class LC1331 {
public static int[] arrayRankTransform(int[] arr) {
int[] temp = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
temp[i] = arr[i];
}
Arrays.sort(temp);
Map<Integer, Integer> map = new HashMap<>();
int rank = 1;
for (int i = 0; i < arr.length; i++) {
if (i > 0 && temp[i] != temp[i - 1]) {
rank++;
}
map.put(temp[i], rank);
}
int[] ranks = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
ranks[i] = map.get(arr[i]);
}
return ranks;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Array Size : ");
int size = sc.nextInt();
System.out.println();
int arr[] = new int[size];
System.out.println("Enter Array Elements : ");
for (int i = 0; i < arr.length; i++) {
System.out.printf("[%d] : ", i);
arr[i] = sc.nextInt();
}
System.out.println();
List<Integer> ans = Arrays.stream(arrayRankTransform(arr)).boxed().collect(Collectors.toList());
System.out.println(ans);
sc.close();
}
}