Design an algorithm to convert a given integer to its Roman numeral representation. Roman numerals are represented by seven different symbols: I
, V
, X
, L
, C
, D
, and M
, which correspond to the values 1, 5, 10, 50, 100, 500, and 1000 respectively.
Roman numerals are typically written from largest to smallest (e.g., 1994 is represented as MCMXCIV: M = 1000, CM = 900, XC = 90, and IV = 4).
However, there are cases where subtraction is used:
I
can be placed before V
(5) and X
(10) to make 4 and 9.X
can be placed before L
(50) and C
(100) to make 40 and 90.C
can be placed before D
(500) and M
(1000) to make 400 and 900.Given an integer within the range of 1 to 3999, write a function that returns its Roman numeral representation.
Examples:
num = 3
Output: "III"
num = 58
Output: "LVIII"
(L = 50, V = 5, III = 3)num = 1994
Output: "MCMXCIV"
(M = 1000, CM = 900, XC = 90, IV = 4)num = 3749
Output: "MMMDCCXLIX"
Explain your approach, its time complexity, and space complexity. Provide code implementation of your solution. Also discuss how you would handle edge cases or constraints in the problem statement.
Given an integer, convert it to a Roman numeral.
The most straightforward approach is to iterate through the decimal place values (1000, 100, 10, 1) and greedily subtract the largest possible Roman numeral value from the input number until the number becomes zero. This method uses a series of if
or while
statements to check each possible value and append the corresponding Roman numeral symbol.
A more efficient and cleaner approach is to use a lookup table (array or dictionary) that maps integer values to their Roman numeral representations. This allows us to iterate through the values in descending order and avoid repetitive checks. The lookup table should include the standard Roman numeral values (1, 5, 10, 50, 100, 500, 1000) as well as the subtractive forms (4, 9, 40, 90, 400, 900).
values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
) and another for their corresponding Roman numeral symbols (e.g., symbols = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
).values
array from highest to lowest.Convert 1994
to a Roman numeral:
1000
: Append "M", number becomes 994
900
: Append "CM", number becomes 94
90
: Append "XC", number becomes 4
4
: Append "IV", number becomes 0
Result: "MCMXCIV"
def int_to_roman(num: int) -> str:
values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
symbols = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
roman_numeral = ""
i = 0
while num > 0:
k = num // values[i]
for _ in range(k):
roman_numeral += symbols[i]
num -= values[i]
i += 1
return roman_numeral
values
and symbols
arrays, regardless of the input number. The result string also has a limited length because the input number is constrained to be between 1 and 3999.