Top Interview 150
Converting integers to Roman numerals is a fun problem involving string manipulation and careful application of Roman numeral rules. Letβs explore LeetCode 12: Integer to Roman, break down the steps, and implement the solution in JavaScript.
π Problem Description
You are given an integer num
(1 β€ num β€ 3999).
Return its Roman numeral representation.
Roman numerals follow specific rules:
- Symbols:
I
(1),V
(5),X
(10),L
(50),C
(100),D
(500),M
(1000). - Use subtractive notation for 4, 9, 40, 90, 400, 900.
π‘ Examples
Example 1
Input: num = 3749
Output: "MMMDCCXLIX"
Explanation:
- 3000 β `MMM`
- 700 β `DCC`
- 40 β `XL`
- 9 β `IX`
Example 2
Input: num = 58
Output: "LVIII"
Explanation:
- 50 β `L`
- 8 β `VIII`
Example 3
Input: num = 1994
Output: "MCMXCIV"
Explanation:
- 1000 β `M`
- 900 β `CM`
- 90 β `XC`
- 4 β `IV`
π JavaScript Solution
To solve this problem, weβll use a greedy approach:
- Start with the largest Roman numeral.
- Subtract its value from the number while appending the numeral to the result.
- Repeat until the number becomes zero.
Implementation
var intToRoman = function(num) {
const romanMap = [
{ value: 1000, symbol: 'M' },
{ value: 900, symbol: 'CM' },
{ value: 500, symbol: 'D' },
{ value: 400, symbol: 'CD' },
{ value: 100, symbol: 'C' },
{ value: 90, symbol: 'XC' },
{ value: 50, symbol: 'L' },
{ value: 40, symbol: 'XL' },
{ value: 10, symbol: 'X' },
{ value: 9, symbol: 'IX' },
{ value: 5, symbol: 'V' },
{ value: 4, symbol: 'IV' },
{ value: 1, symbol: 'I' },
];
let result = '';
for (const { value, symbol } of romanMap) {
while (num >= value) {
result += symbol;
num -= value;
}
}
return result;
};
π How It Works
-
Roman Map:
- Create an ordered list of Roman numeral values and their symbols.
- Include subtractive notations (CM, XC, etc.) in the map.
-
Iterate Through the Map:
- For each numeral, append it to the result while subtracting its value from num until num is less than the numeralβs value.
-
Return the Result:
- After processing all numerals, the result contains the Roman numeral string.
π Complexity Analysis
- > Time Complexity:
O(1)
, because the number of Roman numeral symbols is constant (13). The loop runs a fixed number of iterations regardless of input size. - > Space Complexity:
O(1)
, as we only use a string to build the result.
π Dry Run
Input: num = 1994
Output: "MCMXCIV"
β¨ Pro Tips for Interviews
- Use a Roman map: Predefining values simplifies the algorithm.
- Think about scalability: This solution is efficient since the Roman numeral system has fixed rules and symbols.
- Explain constraints: Confirm the input range (1 to 3999) to clarify assumptions.
π Learn More
Check out the full explanation and code walkthrough on my Dev.to post:
π Roman to Integer - JavaScript Solution
How would you optimize this further? Letβs discuss below! π
Top comments (1)
Follow Me on GitHub π
If you found this solution helpful, check out more of my projects and solutions on my GitHub profile.
Don't forget to follow for more updates!