Leetcode 12. Integer to Roman
Problem
Roman numerals are represented by seven different symbols: I
, V
, X
, L
, C
, D
and M
.
For example, two is written as II
in Roman numeral, just two one's added together. Twelve is written as, XII
, which is simply X
+ II
. The number twenty seven is written as XXVII
, which is XX
+ V
+ II
.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII
. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX
. There are six instances where subtraction is used:
I
can be placed beforeV
(5) andX
(10) to make 4 and 9.X
can be placed beforeL
(50) andC
(100) to make 40 and 90.C
can be placed beforeD
(500) andM
(1000) to make 400 and 900.
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.
Example 1:
Example 2:
Example 3:
Example 4:
Example 5:
Thoughts
As those highly voted discussion posts had figured out, we can manually write out all ten roman digits for each of the ones, tens, hundreds, and thousands places. In this way we hard-coded 40ish digits and used only one line of "code". Admittedly, these hard coded solutions will work perfectly given that roman numerals only range from 1 to 3999.
However, I'd like to suggest an alternative solution that is more general and extensible. Assume some day magically we needs roman numerals to include a very large range of number (e.g. 1 - 99999) and we adds more symbols like "Y, Z, .." besides "X, L, C, D, M". Then it's obvious we shoudn't keep hard coding. That's not what we as programmers do.
Instead, we should optimally only code the RULES that converts integer to roman numerals, as following:
the conversion table: symbols (arbitrary large number) to values, e.g. X to 10 or C to 100.
general rule: largest to smallest from left to right, by adding symbols together.
exception: 4 and 9 case.
Besdies above RULES, the digit by digit conversion should be AUTOMATICALLY performed by computer.
Solution
This solution enables us to easily present arbitarily large range of number in "roman" numerals with new symbols. Instead of hard coding every digits, we only need to include new symbols in the lookup table. (My code should be further improved to be more concise).
In a more pratical sense, it's always good to show interviewers that you have a mindset for extensibility and maintainability.
Complexity Analysis
Time Complexity: O(k), where k equals to the number of digits input number have. We have to iterate all digits of the input number.
Space Complexity: O(1). No additional space is used except the output string builder.
Extension
Simplify the code to make it more readable and concise.
Summary
This question can surely be solved efficiently using brutal force. It's fast and it beats 100%. Nonetheless, these solutions lack the essense of programmic thinking. When we design an algorithm, we should not only care about solving it under given context, but also establishes foundations for future extension if requirement changes.
Reference
Last updated
Was this helpful?