-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmallestNumber.java
More file actions
36 lines (27 loc) · 1.03 KB
/
Copy pathSmallestNumber.java
File metadata and controls
36 lines (27 loc) · 1.03 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
package LeetcodePractice;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class SmallestNumber {
public static int[] smallerNumbersThanCurrent(int[] nums) {
int[] sorted = nums.clone();
Arrays.sort(sorted);
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < sorted.length; i++) {
map.putIfAbsent(sorted[i], i);
}
int[] result = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
result[i] = map.get(nums[i]);
}
return result;
}
public static void main(String[] args) {
int[] nums1 = {8, 1, 2, 2, 3};
System.out.println(Arrays.toString(smallerNumbersThanCurrent(nums1))); // [4, 0, 1, 1, 3]
int[] nums2 = {6, 5, 4, 8};
System.out.println(Arrays.toString(smallerNumbersThanCurrent(nums2))); // [2, 1, 0, 3]
int[] nums3 = {7, 7, 7, 7};
System.out.println(Arrays.toString(smallerNumbersThanCurrent(nums3))); // [0, 0, 0, 0]
}
}