Coding Trainer

Integer to Roman

MediumGreedyk-greedy

Problem

Integer to Roman

Seven symbols represent Roman numerals:

SymbolValue
I1
V5
X10
L50
C100
D500
M1000

Rules:

  • Append the symbol of the maximal value that can be subtracted from the input.
  • Use subtractive forms for 4 and 9: IV, IX, XL, XC, CD, CM.
  • Powers of 10 (I, X, C, M) can appear at most 3 times consecutively.

Given an integer num, convert it to a Roman numeral.

Example 1:

Input:  num = 3749
Output: "MMMDCCXLIX"

Example 2:

Input:  num = 58
Output: "LVIII"

Example 3:

Input:  num = 1994
Output: "MCMXCIV"

Constraints:

  • 1 <= num <= 3999