/12. Integer to Roman

12. Integer to Roman

Medium
Hash Table70.6% acceptance

Given an integer value n in the range [1, 3999], return its Roman numeral representation as a string. Roman numerals are constructed using the following symbols and values: I=1, V=5, X=10, L=50, C=100, D=500, M=1000. Subtractive forms are used for 4 (IV), 9 (IX), 40 (XL), 90 (XC), 400 (CD), and 900 (CM). Only the symbols I, X, C, and M can be repeated up to three times. For other values, use the subtractive form as needed.

Example 1

Input: n=2421

Output: MMCDXXI

Explanation: 2000 = MM, 400 = CD, 20 = XX, 1 = I

Example 2

Input: n=83

Output: LXXXIII

Explanation: 50 = L, 30 = XXX, 3 = III

Example 3

Input: n=999

Output: CMXCIX

Explanation: 900 = CM, 90 = XC, 9 = IX

Constraints

  • 1 <= n <= 3999
  • Input n is an integer
Python (current runtime)

Case 1

Input: n=2768

Expected: 'MMDCCLXVIII'

Case 2

Input: n=145

Expected: 'CXLV'

Case 3

Input: n=3210

Expected: 'MMMCCX'

Case 4

Input: n=4

Expected: 'IV'

Case 5

Input: n=3888

Expected: 'MMMDCCCLXXXVIII'