Integer to Roman

Roman numerals use seven symbols: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500 and M = 1000. Numbers are written from the largest symbol to the smallest and their values add up, except for six subtractive pairs where a smaller symbol sits before a larger one: IV = 4, IX = 9, XL = 40, XC = 90, CD = 400 and CM = 900. Only I, X, C and M may repeat, at most three times in a row. Given an integer num, return its Roman numeral.

30:00

Integer to Roman
medium
Free written solution
Topics
Companies

Roman numerals use seven symbols: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500 and M = 1000. Numbers are written from the largest symbol to the smallest and their values add up, except for six subtractive pairs where a smaller symbol sits before a larger one: IV = 4, IX = 9, XL = 40, XC = 90, CD = 400 and CM = 900. Only I, X, C and M may repeat, at most three times in a row. Given an integer num, return its Roman numeral.

Example 1:
Input: {"num":3749}
Output: MMMDCCXLIX
Constraints:
  • 1≤num≤39991 \leq \text{num} \leq 3999

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"num":3749}

Line the 13 denominations up from largest to smallest, including the six subtractive pairs, and spend 3749 greedily.

num left to convert

3749

of 3749

paid 0 · left 3749

denominations, largest first (purple = subtractive pair)

M

1000

CM

900

D

500

CD

400

C

100

XC

90

L

50

XL

40

X

10

IX

9

V

5

IV

4

I

1

result

empty

Variables
VariableValue
num3749
result""
DepthFunction Call
Stack empty
0/33