LeetCode 179: Largest Number with Custom Comparator

🔥 Day 2 of #100DaysOfLeetCode 📌 Problem: Largest Number — Medium (#179) 🔗 https://lnkd.in/dzV8UhUc 💡 Approach: Normal sorting doesn't work here! The trick is to compare numbers by their concatenation. For "3" and "30" → compare "330" vs "303" → "330" wins → 3 comes first! Used Bubble Sort with a custom comparator — no extra libraries needed. 🐍 My Python Solution: nums = list(map(str, nums)) for i in range(len(nums)):   for j in range(i+1, len(nums)):     if nums[i]+nums[j] < nums[j]+nums[i]:       nums[i], nums[j] = nums[j], nums[i] result = "".join(nums) return "0" if result[0]=="0" else result 📚 What I Learned Today: Sometimes the sorting key isn't the value itself — it's how two elements behave TOGETHER. Concatenation comparison is a powerful trick for number ordering problems! ⚡ Time : O(n²) — Bubble Sort 📦 Space : O(n) — String conversion #LeetCode #Python #DSA #100DaysOfLeetCode #100DaysOfCode #CodingJourney #DataScience #ProblemSolving #Programming #CS

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories