-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.java
More file actions
24 lines (22 loc) · 890 Bytes
/
Copy pathsolution.java
File metadata and controls
24 lines (22 loc) · 890 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
void sortBySetBitCount(int[] arr) {
// I create 32 buckets so each possible set-bit count has its own place.
ArrayList<Integer>[] buckets = new ArrayList[32];
for (int i = 0; i < 32; i++) {
buckets[i] = new ArrayList<>();
}
// I push each number into the bucket that matches its set-bit count.
// Appending in order keeps the sort stable for equal counts.
for (int num : arr) {
buckets[Integer.bitCount(num)].add(num);
}
// I write the values back from the highest bucket to the lowest bucket.
// This directly updates the original array in the required order.
int idx = 0;
for (int bits = 31; bits >= 0; bits--) {
for (int num : buckets[bits]) {
arr[idx++] = num;
}
}
}
}