math
strings
greedy

Roman numerals are written with seven symbols: I (1), V (5), X (10), L (50), C (100), D (500), and M (1000).

Numbers are generally written largest symbol first. The exception is six subtractive cases, where a smaller symbol precedes a larger one to mean subtraction:

  • IV = 4 and IX = 9
  • XL = 40 and XC = 90
  • CD = 400 and CM = 900

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

Input / output

  • Input: num: int
  • Output: string — the Roman numeral

Constraints

  • 1 <= num <= 3999

Follow-up

The thirteen value/symbol pairs (including the six subtractive ones) are chosen so a single greedy pass always produces the canonical numeral. Why does greedily subtracting the largest usable value never paint you into a corner?

Examples

Example 1

Input: num = 3
Output: "III"

Example 2

Input: num = 58
Output: "LVIII"

Example 3

Input: num = 1994
Output: "MCMXCIV"