LeetCode Entry
1356. Sort Integers by The Number of 1 Bits
Sort an array comparing by bit count and value
1356. Sort Integers by The Number of 1 Bits easy
blog post
substack

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/386
Problem TLDR
Sort an array comparing by bit count and value
Intuition
Let’s use some Kotlin API
Approach
countOneBitssortedWithcompareBy
Complexity
-
Time complexity: \(O(nlog(n))\)
-
Space complexity: \(O(n)\)
Code
fun sortByBits(arr: IntArray): IntArray = arr
.sortedWith(compareBy({ it.countOneBits() }, { it }))
.toIntArray()