Integer to Roman
Medium
Topics
Roman numerals are written by adding up symbols from largest to smallest: I 1, V 5, X 10, L 50, C 100, D 500, M 1000.
Six values are written by subtraction instead, placing a smaller symbol before a larger one: IV 4, IX 9, XL 40, XC 90, CD 400, CM 900. No other subtractive pair is allowed — 99 is XCIX, never IC.
Given an integer n, return its Roman numeral.
Example 1
Input: n = 3749 Output: "MMMDCCXLIX" Explanation: 3000 is MMM, 700 is DCC, 40 is XL and 9 is IX.
Example 2
Input: n = 58 Output: "LVIII" Explanation: 50 is L, 5 is V, and 3 is III.
Example 3
Input: n = 1994 Output: "MCMXCIV" Explanation: M, then CM for 900, XC for 90 and IV for 4 — three subtractive pairs in one numeral.
Constraints
- 1 <= n <= 3999
Editorial — intuition, approach, complexity and pitfalls
Intuition
The six subtractive pairs look like special cases that need their own branch, and the trick is to stop treating them as special. If CM is simply another symbol worth 900, sitting in the table between M at 1000 and D at 500, then the rule for the whole numeral collapses to one sentence: repeatedly take the largest symbol that still fits.
That works because of a property of this particular table — with the six pairs included, the greedy choice is always right. There is no value where taking the largest fitting symbol leads to a longer numeral than some cleverer choice, which is why no search or backtracking is needed.
The upper bound of 3999 is why M never needs a subtractive partner: four thousand would require a symbol above M to subtract from, and there is none.
Approach
- Build a table of 13 symbols with their values, in descending order: 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.
- Walk the table from the top. While the remaining number is at least the current value, append that symbol and subtract its value.
- Move to the next table entry and continue until the number reaches zero.
Walkthrough
n = 1994
value symbol fits? remaining output
1000 M yes 994 M
900 CM yes 94 MCM
500 D no 94 MCM
400 CD no 94 MCM
100 C no 94 MCM
90 XC yes 4 MCMXC
50 L no 4 MCMXC
40 XL no 4 MCMXC
10 X no 4 MCMXC
9 IX no 4 MCMXC
5 V no 4 MCMXC
4 IV yes 0 MCMXCIV
answer MCMXCIVComplexity
- Time — O(1): the table has 13 entries and no symbol repeats more than three times, so the loop body runs a bounded number of times whatever the input.
- Space — O(1): the output is at most 15 characters for any number under 4000.
Common Pitfalls
- Leaving the six subtractive pairs out of the table and special-casing digits 4 and 9 afterwards. It works, and it is four times the code.
- Putting the table in the wrong order. The greedy choice depends on descending value — 900 must come before 500, not after.
- Inventing subtractive pairs the system does not have.
ICfor 99 andIMfor 999 are not Roman numerals; only the six listed pairs exist. - Using
ifrather thanwhileon each symbol, which caps every symbol at one use and turns 3000 intoM.
Full Solution — all languages
class Solution:
def intToRoman(self, n):
VALUES = [
(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"),
]
out = []
for value, symbol in VALUES:
while n >= value:
out.append(symbol)
n -= value
return "".join(out)import java.util.*;
class Solution {
public String intToRoman(int n) {
int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
StringBuilder out = new StringBuilder();
for (int i = 0; i < values.length; i++) {
while (n >= values[i]) {
out.append(symbols[i]);
n -= values[i];
}
}
return out.toString();
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: string intToRoman(int n) {
int values[13] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
string symbols[13] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
string out;
for (int i = 0; i < 13; i++) {
while (n >= values[i]) {
out += symbols[i];
n -= values[i];
}
}
return out;
}
};/**
* @param {number} n
* @return {string}
*/ var intToRoman = function(n) {
const VALUES = [ [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"], ];
let out = "";
for (const pair of VALUES) {
while (n >= pair[0]) {
out += pair[1];
n -= pair[0];
}
}
return out;
};