Integer to Roman

Medium

Topics
Hash TableMathString

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

piecesM = 1000M = 1000M = 1000D = 500C = 100C = 100XL = 40IX = 9
answerMMMDCCXLIX
written by subtraction, smaller symbol first
The numeral is the pieces above read left to right, largest value first. Here 2 pieces use the subtractive form, where a smaller symbol sits before a larger one; only six such pairs exist and no others are legal. The value adds up to 3749.
Each chip is one symbol of the answer and the value it contributes.
Input:  n = 3749
Output: "MMMDCCXLIX"
Explanation: 3000 is MMM, 700 is DCC, 40 is XL and 9 is IX.

Example 2

piecesL = 50V = 5I = 1I = 1I = 1
answerLVIII
This number needs none of the six subtractive pairs — every piece is a plain symbol.
Input:  n = 58
Output: "LVIII"
Explanation: 50 is L, 5 is V, and 3 is III.

Example 3

piecesM = 1000CM = 900XC = 90IV = 4
answerMCMXCIV
written by subtraction, smaller symbol first
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

  1. 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.
  2. Walk the table from the top. While the remaining number is at least the current value, append that symbol and subtract its value.
  3. 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 MCMXCIV

Complexity

  • 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. IC for 99 and IM for 999 are not Roman numerals; only the six listed pairs exist.
  • Using if rather than while on each symbol, which caps every symbol at one use and turns 3000 into M.

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)
SensAI
Beta
Tap the mic to stop voice mode