Integer to Roman – Solution & Complexity
Solution Walkthrough
Step 1: Model the symbol table
- List the thirteen value/symbol pairs in descending order of value, with the six subtractive forms (
CM,CD,XC,XL,IX,IV) interleaved at their exact values. - Including the subtractive pairs directly in the table is what lets a plain greedy loop emit canonical numerals without special cases.
- List the thirteen value/symbol pairs in descending order of value, with the six subtractive forms (
Step 2: Greedily subtract
- Walk the table from largest value to smallest.
- While the remaining number is at least the current value, append that symbol and subtract the value.
- Move to the next pair when the current one no longer fits.
Step 3: Why greedy is safe here
- Each ordinary symbol repeats at most three times before the next subtractive form takes over (e.g.
IIIthenIV), so the table encodes the "no four-in-a-row" rule implicitly. - Taking the largest usable value can never force an invalid or longer representation, because the smaller values can always cover whatever remainder is left.
- Each ordinary symbol repeats at most three times before the next subtractive form takes over (e.g.
Step 4: Optimal greedy solution
- Build the result string in one descending pass.
- Time is
O(1)because the table size is fixed and the number is bounded by 3999; space isO(1)beyond the output.
Step 5: Common mistakes
- Forgetting the subtractive pairs and emitting
IIIIinstead ofIV. - Using
ifinstead ofwhile, which stops after a singleMwhen the number needs several. - Listing the table out of order so a larger symbol is reachable after a smaller one was already applied.
- Forgetting the subtractive pairs and emitting