-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeightChecker.java
31 lines (30 loc) · 917 Bytes
/
HeightChecker.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
class Solution {
public int heightChecker(int[] heights) {
int res = 0;
int[] clone = heights.clone();
countingSort(clone);
for (int i = 0; i < heights.length; i++){
if (heights[i] != clone[i])
res++;
}
return res;
}
private void countingSort(int[] arr){
int maxVal, minVal;
maxVal = minVal = arr[0];
HashMap<Integer, Integer> freqs = new HashMap<>();
for (int num : arr){
freqs.put(num, freqs.getOrDefault(num, 0) + 1);
maxVal = Math.max(maxVal, num);
minVal = Math.min(minVal, num);
}
int idx = 0;
for (int val = minVal; val <= maxVal; val++){
while (freqs.getOrDefault(val, 0) > 0){
arr[idx] = val;
freqs.put(val, freqs.get(val) - 1);
idx++;
}
}
}
}