Solving 12. Integer to Roman on LeetCode with Greedy Algorithm

🚀 Day 66 of #100DaysOfCode Solved 12. Integer to Roman on LeetCode 🔗 🧠 Key Insight: Roman numerals are built using fixed value-symbol pairs 👉 Including special cases like IV (4), IX (9), XL (40), CM (900) ⚙️ Approach (Greedy - Largest to Smallest): 1️⃣ Maintain two arrays: 🔹 Values → [1000, 900, 500, 400, ..., 1] 🔹 Symbols → ["M", "CM", "D", "CD", ..., "I"] 2️⃣ Traverse values from largest to smallest 3️⃣ For each value: 🔹 While num >= value[i]:   • Append corresponding symbol   • Subtract value from num 4️⃣ Stop when num == 0 ⏱️ Time Complexity: O(1) 📦 Space Complexity: O(1) 💡 Edge Cases: ✔️ 4 → "IV" ✔️ 9 → "IX" ✔️ 40 → "XL" ✔️ 900 → "CM" #100DaysOfCode #LeetCode #DSA #Greedy #Strings #Java #InterviewPrep #CodingJourney

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories