[
bit manipulation
sort
]
Leetcode 1356. Sort Integers by The Number of 1 Bits
Problem statement
https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits/
Solution
Just do what is asked.
Complexity
It is O(n log n + n log M)
for time and O(n)
for space.
Code
class Solution:
def sortByBits(self, A):
return sorted(A, key = lambda x: (bin(x).count("1"), x))