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:
- If the value does not start with 4 or 9, select the symbol of the maximal value that can be subtracted from the input, append that symbol to the result, subtract its value, and convert the remainder to a Roman numeral.
-
If the value starts with 4 or 9 use the subtractive form representing one symbol subtracted from the following symbol,
for example, 4 is 1 (
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 ). -
Only powers of 10 (
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.
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
Output: "LVIII"
Explanation: 50 = L
8 = VIII
Output: "MCMXCIV"
Explanation: 1000 = M
900 = CM
90 = XC
4 = IV
Constraints:
1 <= num <= 3999
Contents
Solution 1 - Add extra special cases into Roman Symbol table
As per the problem statement, there are six special cases when converting integer to roman representation, so in this approach, we will add the extra entries into roman to integer table and then divide the input number possible with highest number to lowest in the integer int the roman to integer mapping, once divided then continue the process with the remainder.
After updating the roman to integer, mapping will look like this
Symbol | I | IV | V | IX | X | XL | L | XC | C | CD | D | CM | M |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Value | 1 | 4 | 5 | 9 | 10 | 40 | 50 | 90 | 100 | 400 | 500 | 900 | 1000 |
Now, let's apply above mapping to a an example
-
First, we will try to divide using the highest number in roman to integer table, which is
1000 and we can divide input number 3 times so we will addMMM to the result, and update thenum with remainder, which is749 -
Next biggest number in roman to integer table is
900 , and we cannot divide749 using this. -
Next biggest number in roman to integer table is
500 , and we can divide749 one time, so addD to the result, so the result now becomesMMMD and remainder will become249 . -
Next biggest number in roman to integer table is
400 , and we cannot divide249 with this. -
Next biggest number in roman to integer table is
100 , and we can divide249 two times, so addCC to the result, so the result now becomesMMMDCC and remainder will become49 . -
Next biggest number in roman to integer table is
90 , and we cannot divide49 with this. -
Next biggest number in roman to integer table is
50 , and we cannot divide49 with this. -
Next biggest number in roman to integer table is
40 , and we can divide49 one time, so addXL to the result, so the result now becomesMMMDCCXL and remainder will become9 . -
Next biggest number in roman to integer table is
10 , and we cannot divide9 with this. -
Next biggest number in roman to integer table is
9 , and we can divide9 one time, so addIX to the result, so the result now becomesMMMDCCXLIX and remainder will become0 .
Complexity Analysis:
Time complexity: Above code runs in O(1) time since we are only iterating through roman to integer mapping array, which is constant.
Space complexity: O(1).
Above implementations source code can be found at