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 <= 3999
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to convert a regular number into its Roman numeral representation. The brute force method considers all possible combinations of Roman numeral symbols until the correct representation is found. It's like trying out different building blocks to get the right number.
Here's how the algorithm would work step-by-step:
def integer_to_roman(number):
roman_values = [
1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1
]
roman_symbols = [
"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"
]
roman_numeral = ""
# Iterate through each value and corresponding numeral
for i in range(len(roman_values)):
# Determine how many times current value can be subtracted
while number >= roman_values[i]:
# Append numeral to the result
roman_numeral += roman_symbols[i]
# Reduce the input number
number -= roman_values[i]
return roman_numeral
The best way to convert an integer to a Roman numeral is to use pre-defined values for each Roman symbol. We process the integer from largest to smallest place values, finding the corresponding Roman symbol for each value.
Here's how the algorithm would work step-by-step:
def integer_to_roman(input_integer):
roman_map = {
1: 'I', 4: 'IV', 5: 'V', 9: 'IX', 10: 'X',
40: 'XL', 50: 'L', 90: 'XC', 100: 'C',
400: 'CD', 500: 'D', 900: 'CM', 1000: 'M'
}
integer_values = list(roman_map.keys())
integer_values.sort(reverse=True)
roman_numeral = ''
for integer_value in integer_values:
# We want to process the largest to smallest values
while input_integer >= integer_value:
roman_numeral += roman_map[integer_value]
# Subtract the integer value from the number
input_integer -= integer_value
return roman_numeral
Case | How to Handle |
---|---|
Input is 1 | The algorithm should correctly return 'I' for the smallest valid input. |
Input is 3999 | The algorithm should correctly return 'MMMCMXCIX' for the largest valid input. |
Input contains a number requiring subtraction (e.g., 4, 9, 40, 90, 400, 900) | Ensure the subtraction logic is correctly applied to handle these cases. |
Input is a multiple of 1000 (e.g., 1000, 2000, 3000) | Ensure the algorithm correctly handles multiples of 1000 without mixing with other numeral symbols. |
Input close to a subtraction threshold (e.g., 8, 11, 38, 41, 88, 91, 398, 401, 898, 901) | Verify the correct combination of additive and subtractive symbols around these values. |
Input that requires many repeated symbols (e.g., 3, 30, 300, 3000) | Ensure that the algorithm doesn't exceed the allowed repetition of each symbol. |
Input is 0 or negative | The problem statement specifies input range 1-3999; reject inputs outside this range with an exception or error code. |
Non-integer input (e.g., string, float) | Check if input is an integer and throw an exception or return an error code if it is not. |