Seven different symbols represent Roman numerals with the following values:
| Symbol | Value |
|---|---|
| I | 1 |
| V | 5 |
| X | 10 |
| L | 50 |
| C | 100 |
| D | 500 |
| M | 1000 |
Roman numerals are formed by appending the conversions of decimal place values from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:
I) less than 5 (V): IV and 9 is 1 (I) less than 10 (X): IX. Only the following subtractive forms are used: 4 (IV), 9 (IX), 40 (XL), 90 (XC), 400 (CD) and 900 (CM).I, X, C, M) can be appended consecutively at most 3 times to represent multiples of 10. You cannot append 5 (V), 50 (L), or 500 (D) multiple times. If you need to append a symbol 4 times use the subtractive form.Given an integer, convert it to a Roman numeral.
Example 1:
Input: num = 3749
Output: "MMMDCCXLIX"
Explanation:
3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places
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
Constraints:
1 <= num <= 3999Problem Overview: Given an integer between 1 and 3999, convert it into its Roman numeral representation. Roman numerals use combinations of symbols like I, V, X, L, C, D, and M, including subtractive pairs such as IV (4) and IX (9).
Approach 1: Greedy Value-Symbol Mapping (Time: O(1), Space: O(1))
The most practical solution uses a greedy strategy. Create an ordered mapping of integer values to their Roman symbols: 1000 → M, 900 → CM, 500 → D, 400 → CD, 100 → C, 90 → XC, 50 → L, 40 → XL, 10 → X, 9 → IX, 5 → V, 4 → IV, and 1 → I. Iterate through this list from largest to smallest. For each value, repeatedly append the corresponding symbol while the remaining number is greater than or equal to that value, subtracting the value each time.
The key insight is that Roman numerals are constructed using the largest possible symbol at every step. Handling subtractive cases like 900, 400, and 9 directly in the mapping removes special-case logic. Because the Roman numeral range is capped at 3999, the number of iterations is constant, giving effective O(1) time and space complexity. The mapping itself is often stored using arrays or a small hash table, and the final result is built using string concatenation.
Approach 2: Digit-by-Digit Lookup (Time: O(1), Space: O(1))
Another clean approach processes the number by place value. Precompute arrays for thousands, hundreds, tens, and ones. For example, the tens array contains values like "", "X", "XX", "XXX", "XL", and so on. Extract each digit using division and modulo operations from math operations, then concatenate the corresponding Roman string from each lookup table.
This approach avoids loops and repeated subtraction entirely. Each digit maps directly to a prebuilt Roman fragment. Runtime remains constant because the number of digits is fixed (maximum four). The tradeoff is slightly more upfront setup with lookup arrays, but the final code is extremely fast and predictable.
Recommended for interviews: The greedy mapping approach is what most interviewers expect. It mirrors how Roman numerals are actually constructed and demonstrates clear reasoning about ordered values and subtractive notation. A brute-force mindset—trying to build symbols character by character—shows basic understanding, but the greedy mapping shows stronger algorithmic thinking and cleaner implementation.
This approach utilizes a greedy strategy by iterating over an ordered list of numeral values and corresponding symbols. Starting from the largest value, it repeatedly subtracts from the given number until the result is less, appending the equivalent symbol each time. This ensures the numeral is created with the fewest characters.
The C implementation defines an array of integer values and corresponding Roman symbols. A loop iterates through these values, subtracting the largest possible from the input number, and appends the associated Roman symbol to the result string.
C++
Java
Python
C#
JavaScript
Time Complexity: O(1) — The solution has a fixed iteration through a constant list of length 13, hence it's O(1).
Space Complexity: O(1) — Uses constant space for the result string and numeral arrays.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Greedy Value-Symbol Mapping | O(1) | O(1) | Best general solution. Mirrors Roman numeral construction and is commonly expected in interviews. |
| Digit-by-Digit Lookup Tables | O(1) | O(1) | Useful when you prefer direct indexing without loops or subtraction logic. |
Roman to Integer - Leetcode 13 - Python • NeetCode • 197,307 views views
Watch 9 more video solutions →Practice Integer to Roman with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor