CodingNodes / Arrays & Hashing / Integer to Roman System design Space play · ←→ step · R restart

Problem

Roman numerals use the symbols I 1, V 5, X 10, L 50, C 100, D 500 and M 1000. Symbols are written from largest to smallest and added together, with six exceptions that subtract instead: IV 4, IX 9, XL 40, XC 90, CD 400 and CM 900.

Given an integer num, return its roman numeral as a string.

Constraints

  • 1 ≤ num ≤ 3999

Examples: pick one to animate

Solutions

TimeO(1) SpaceO(1)

Time: The table has a fixed 13 entries, and num is at most 3999, so at most 15 symbols are ever written.

Space: The table has a fixed size and the answer is at most 15 characters long.

List every value that has its own symbol, including the subtractive pairs, from largest to smallest. For each value, write its symbol and subtract it from num for as long as it still fits.