Sort Integers by Binary 1 Bits

🚀 Today’s LeetCode Problem — Sort Integers by Number of 1 Bits Let’s solve today’s LC in story mode 🎭 Imagine numbers are contestants in a Binary Costume Contest. Today’s contestants: 👉 [5, 3, 7] But this contest has a twist 👇 👨⚖️ The judge doesn’t care about the number itself first. He cares about… 🎈 How many 1s are in their binary costume 🎭 What Are They Wearing? 🔹 5 → 101 → 🎈🎈 (2 ones) 🔹 3 → 011 → 🎈🎈 (2 ones) 🔹 7 → 111 → 🎈🎈🎈 (3 ones) 🏆 Judge’s Rules 1️⃣ Fewer 1s → Higher priority 2️⃣ Same number of 1s → Smaller number wins 👉 Step 1: 3 & 5 → 2 ones 7 → 3 ones So 7 goes last. 👉 Step 2: Tie between 3 and 5 Smaller number first → 3 before 5 ✅ Final Answer: [3, 5, 7] 💡 How I Solved It I implemented this using: 🔹 Brian Kernighan’s Algorithm for efficient bit counting 🔹 TreeMap to maintain sorted order by bit count + value 🔹 Also compared with built-in bitCount() + custom comparator sorting This ensures: ✔ Efficient bit counting ✔ Clean tie-breaking ✔ Optimal sorting logic If you’re interested, I’ve pushed the complete solution (Kernighan + TreeMap + built-in approach) : https://lnkd.in/gV9HZ9w4 👨💻 Would love feedback from fellow developers 🙌 #LeetCode #DSA #Java #ProblemSolving #CodingJourney #100DaysOfCode #Tech #SoftwareEngineering

  • graphical user interface, calendar

To view or add a comment, sign in

Explore content categories