Algorithm Day107 - Integer to Roman
📌 Problem
Convert an integer to a Roman numeral. Input is guaranteed to be within the range 1 <= num <= 3999.
💬 Examples
Example 1
1 | Input: num = 3 |
Example 2
1 | Input: num = 58 |
Example 3
1 | Input: num = 1994 |
💡 Intuition (Greedy)
Use a greedy approach: repeatedly subtract the largest Roman value that does not exceed the current number and append its symbol. Predefine pairs of values and symbols in decreasing order (including subtractive forms like 900 -> “CM”).
🔢 Java Code (Greedy)
1 | class Solution { |
⏱ Complexity Analysis
- Time: O(1) — bounded by a small constant (max ~15 symbol appends).
- Space: O(1) — output size is O(1) relative to input bounds.
✍️ Summary
- Predefine Roman symbols including subtractive pairs.
- Greedy subtraction + append produces correct Roman numeral efficiently.
- Java implementation above is simple and interview-friendly.