-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
30 lines (27 loc) · 954 Bytes
/
Copy pathsolution.cpp
File metadata and controls
30 lines (27 loc) · 954 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
25
26
27
28
29
30
class Solution
{
public:
vector<int> sortBySetBitCount(vector<int> &arr)
{
// I keep 32 buckets because a standard positive integer can have at most 32 set bits.
vector<vector<int>> buckets(32);
// I place each number into the bucket that matches its set-bit count.
// This keeps the original order inside each bucket, so ties remain stable.
for (int num : arr)
{
buckets[__builtin_popcount((unsigned int)num)].push_back(num);
}
// I rebuild the answer from the highest set-bit count to the lowest.
// That gives descending order by bit count while preserving tie order.
vector<int> ans;
ans.reserve(arr.size());
for (int bits = 31; bits >= 0; --bits)
{
for (int num : buckets[bits])
{
ans.push_back(num);
}
}
return ans;
}
};