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 <= 3999When 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:
To solve this problem using a brute force strategy, you would exhaustively search through all possible combinations of Roman numeral symbols to see which ones add up to your target number. You would essentially try building the Roman numeral one piece at a time by testing every available symbol combination until you find the exact match.
Here's how the algorithm would work step-by-step:
def int_to_roman(target_number):
roman_symbols = [("M", 1000), ("CM", 900), ("D", 500), ("CD", 400), ("C", 100), ("XC", 90), ("L", 50), ("XL", 40), ("X", 10), ("IX", 9), ("V", 5), ("IV", 4), ("I", 1)]
def backtrack(current_target, current_result):
# We reach the base case when the target number is fully reduced to zero
if current_target == 0:
return current_result
for symbol, value in roman_symbols:
# Only attempt to subtract and recurse if the symbol fits within the current target
if current_target >= value:
result_from_recursion = backtrack(current_target - value, current_result + symbol)
# If a valid path to zero is found, propagate the successful result upward
if result_from_recursion:
return result_from_recursion
# Backtrack by returning empty if none of the symbol choices lead to a valid solution
return ""
return backtrack(target_number, "")To convert any number into a Roman numeral, the optimal strategy mimics how humans naturally think about value by constantly subtracting the largest possible Roman symbol values until nothing is left. By pre-defining all standard symbol combinations and their special subtraction pairs in descending order, we can greedily build the Roman numeral from left to right.
Here's how the algorithm would work step-by-step:
def int_to_roman(given_integer):
roman_value_pairs = [
(1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'),
(100, 'C'), (90, 'XC'), (50, 'L'), (40, 'XL'),
(10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I')
]
accumulated_roman_numerals = []
# Descending order ensures we use the largest symbols possible first.
for symbol_value, roman_symbol in roman_value_pairs:
# Keep subtracting the same value while it fits to maximize symbol usage.
while given_integer >= symbol_value:
accumulated_roman_numerals.append(roman_symbol)
given_integer -= symbol_value
# Combining all collected symbols forms the correct final Roman numeral string.
return ''.join(accumulated_roman_numerals)| Case | How to Handle |
|---|---|
| Minimum valid input of 1 | The greedy algorithm directly matches the value to I and terminates. |
| Maximum constraint input of 3999 | The predefined symbol list up to M allows correct translation without overflow or array out of bounds. |
| Numbers requiring subtractive notation like 4, 9, 40, or 90 | Including composite pairs like IV and IX in our descending lookup table ensures these are naturally picked by the greedy subtraction. |
| Numbers with zero in various place values such as 100 | The algorithm simply skips symbols whose values are greater than the remaining number, handling zeros implicitly. |
| Inputs outside the standard Roman numeral range such as 0 or negative numbers | Constraint specifications guarantee standard inputs from 1 to 3999, but defensive checks can return an empty string for non-positive values. |
| Repeated identical highest digits like 3000 | The greedy subtraction loop applies the largest symbol M repeatedly until the remainder is smaller than 1000. |