Integer to Roman
Array, Iterative Approach
Roman numerals are represented by seven different symbols: I
, V
, X
, L
, C
, D
and M
.
For example, 2
is written as II
in Roman numeral, just two one's added together. 12
is written as XII
, which is simply X + II
. The number 27
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.
Example 1:
Example 2:
Example 3:
Constraints:
1 <= num <= 3999
Solutions
The problem is to convert an integer to a Roman numeral. The solution involves a process of repeatedly subtracting the largest possible Roman numeral value from the input number and appending the corresponding Roman numeral to the result. This process continues until the input number has been reduced to zero. The final result is the Roman numeral representation of the original input number. The solution also includes a validation step to ensure that the input number is within the valid range for Roman numerals (1 to 3999). If the input number is outside this range, the function returns an empty string.
Approach - Brute Force Technique
Checks each numeral in the dictionary for every subtraction until the number becomes zero.
Steps
Dictionary Initialization: A dictionary
romanLatters
is initialized with integer values as keys and their corresponding Roman numerals as values. The dictionary is sorted in descending order of keys.Input Validation: The
IsValidInput
function checks if the input numbernum
is within the valid range (1 to 3999). If the input number is outside this range, the function returnsfalse
, and theIntToRoman
function will return an empty string.Roman Numeral Conversion: The
IntToRoman
function converts the input number to a Roman numeral. It initializes an empty stringresult
to store the Roman numeral.Iteration Over Dictionary: The function enters a
while
loop that continues as long as the current numbernum
is not zero. Inside thewhile
loop, it iterates over each item in theromanLatters
dictionary.Subtraction and Appending: For each dictionary item, it checks if the current number
num
is greater than or equal to the key of the item. If it is, it appends the value of the item (the Roman letter) toresult
and subtracts the key of the item (the Roman numeral) fromnum
. Then it breaks theforeach
loop and goes back to thewhile
loop.Next Dictionary Item: If
num
is not zero and is less than the key of the current dictionary item, the function moves to the next item in the dictionary and repeats the process.Completion: The function continues this process until
num
becomes zero. At this point, all the Roman numerals that make up the input number have been appended toresult
.Return Result: Finally, the function returns the Roman numeral as a string.
Complexity
Time Complexity: O(1)
Auxiliary Space: O(1)
Time Complexity: The time complexity is O(1). This is because the number of Roman numerals is fixed (13 numerals), and the maximum possible number is also fixed (3999). Therefore, the number of iterations does not grow with the size of the input number, making the time complexity constant.
Space Complexity: The space complexity is also O(1). The space used by the program does not grow with the size of the input number. The dictionary of Roman numerals uses a constant amount of space, and the string result
will at most hold the Roman numeral equivalent of the number 3999, which is a fixed length string.
Please note that this analysis assumes that dictionary lookups, string concatenation, and integer comparisons/subtractions can be done in constant time.
Approach - Iterative Technique
Iterates over the dictionary and for each key-value pair, it performs a loop operation until the number is less than the key.
Steps
Dictionary Initialization: A dictionary
romanLatters
is initialized with integer values as keys and their corresponding Roman numerals as values. The dictionary is sorted in descending order of keys.Input Validation: The
IsValidInput
function checks if the input numbernum
is within the valid range (1 to 3999). If the input number is outside this range, the function returnsfalse
, and theIntToRoman
function will return an empty string.Roman Numeral Conversion: The
IntToRoman
function converts the input number to a Roman numeral. It initializes an emptyStringBuilder
objectresult
to store the Roman numeral.Iteration Over Dictionary: The function iterates over each item in the
romanLatters
dictionary. For each item, it enters awhile
loop that continues as long as the current numbernum
is greater than or equal to the key of the current dictionary item.Subtraction and Appending: Inside the
while
loop, the function appends the value of the current dictionary item (the Roman letter) toresult
and subtracts the key of the current dictionary item (the Roman numeral) fromnum
. This process continues untilnum
becomes less than the key of the current dictionary item.Next Dictionary Item: Once
num
becomes less than the key of the current dictionary item, the function moves to the next item in the dictionary and repeats the process.Completion: The function continues this process until
num
becomes zero. At this point, all the Roman numerals that make up the input number have been appended toresult
.Return Result: Finally, the function returns the Roman numeral as a string by calling the
ToString
method onresult
.
Complexity
Time Complexity: O(1)
Auxiliary Space: O(1)
Time Complexity: The time complexity is O(1). This is because the number of Roman numerals is fixed (13 numerals), and the maximum possible number is also fixed (3999). Therefore, the number of iterations does not grow with the size of the input number, making the time complexity constant.
Space Complexity: The space complexity is also O(1). The space used by the program does not grow with the size of the input number. The dictionary of Roman numerals uses a constant amount of space, and the StringBuilder
object result
will at most hold the Roman numeral equivalent of the number 3999, which is a fixed length string.
Please note that this analysis assumes that dictionary lookups, StringBuilder
append operations, and integer comparisons/subtractions can be done in constant time.
Last updated