|
| 1 | +--- |
| 2 | +id: Integer to Roman |
| 3 | +title: Integer to Roman (LeetCode) |
| 4 | +sidebar_label: 0012-Integer to Roman |
| 5 | +tags: |
| 6 | + - Hash Table |
| 7 | + - Math |
| 8 | + - String |
| 9 | +description: Convert a given integer to a Roman numeral using specific rules for the Roman numeral system. |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 15 | +| :----------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------ | :------------------------------------------------- | |
| 16 | +| [Integer to Roman](https://leetcode.com/problems/integer-to-roman/) | [Integer to Roman Solution on LeetCode](https://leetcode.com/problems/integer-to-roman/solutions/3216797/easiest-beginner-friendly-sol-c-java-python/) | [gabaniyash846](https://leetcode.com/u/gabaniyash846/) | |
| 17 | + |
| 18 | +## Problem Statement |
| 19 | + |
| 20 | +Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D`, and `M`. |
| 21 | + |
| 22 | +| Symbol | Value | |
| 23 | +| :----- | :---- | |
| 24 | +| I | 1 | |
| 25 | +| V | 5 | |
| 26 | +| X | 10 | |
| 27 | +| L | 50 | |
| 28 | +| C | 100 | |
| 29 | +| D | 500 | |
| 30 | +| M | 1000 | |
| 31 | + |
| 32 | +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: |
| 33 | + |
| 34 | +- `I` can be placed before `V` (5) and `X` (10) to make 4 and 9. |
| 35 | +- `X` can be placed before `L` (50) and `C` (100) to make 40 and 90. |
| 36 | +- `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900. |
| 37 | + |
| 38 | +Given an integer, convert it to a Roman numeral. |
| 39 | + |
| 40 | +### Examples |
| 41 | + |
| 42 | +**Example 1:** |
| 43 | + |
| 44 | +- **Input**: `num = 3749` |
| 45 | +- **Output**: `"MMMDCCXLIX"` |
| 46 | +- **Explanation**: |
| 47 | + - 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) |
| 48 | + - 700 = DCC as 500 (D) + 100 (C) + 100 (C) |
| 49 | + - 40 = XL as 10 (X) less than 50 (L) |
| 50 | + - 9 = IX as 1 (I) less than 10 (X) |
| 51 | + |
| 52 | +**Example 2:** |
| 53 | + |
| 54 | +- **Input**: `num = 58` |
| 55 | +- **Output**: `"LVIII"` |
| 56 | +- **Explanation**: |
| 57 | + - 50 = L |
| 58 | + - 8 = VIII |
| 59 | + |
| 60 | +**Example 3:** |
| 61 | + |
| 62 | +- **Input**: `num = 1994` |
| 63 | +- **Output**: `"MCMXCIV"` |
| 64 | +- **Explanation**: |
| 65 | + - 1000 = M |
| 66 | + - 900 = CM |
| 67 | + - 90 = XC |
| 68 | + - 4 = IV |
| 69 | + |
| 70 | +### Constraints |
| 71 | + |
| 72 | +- `1 <= num <= 3999` |
| 73 | + |
| 74 | +## Solution |
| 75 | + |
| 76 | +### Approach |
| 77 | + |
| 78 | +#### Intuition |
| 79 | + |
| 80 | +To convert an integer to a Roman numeral, we need to repeatedly subtract the largest possible Roman numeral value from the integer while appending the corresponding symbol to the result string. We use predefined pairs of integers and their Roman numeral representations to guide this process. |
| 81 | + |
| 82 | +#### Algorithm |
| 83 | + |
| 84 | +1. **Initialize the Roman numeral string**: |
| 85 | + - Create an empty string `Roman` to store the resulting Roman numeral. |
| 86 | + |
| 87 | +2. **Create a list of integer-Roman pairs**: |
| 88 | + - Use a list of pairs to store the values and symbols of Roman numerals in descending order of values. |
| 89 | + |
| 90 | +3. **Iterate through the list of pairs**: |
| 91 | + - For each pair, check if the input integer is greater than or equal to the Roman numeral value. |
| 92 | + - If it is, add the corresponding symbol to the `Roman` string and subtract the corresponding value from the input integer. |
| 93 | + - Repeat this process until the input integer becomes zero. |
| 94 | + |
| 95 | +4. **Return the Roman numeral string**: |
| 96 | + - After processing all the pairs, return the `Roman` string containing the converted Roman numeral. |
| 97 | + |
| 98 | +### Code |
| 99 | + |
| 100 | +#### C++ Implementation |
| 101 | + |
| 102 | +```cpp |
| 103 | +class Solution { |
| 104 | +public: |
| 105 | + string intToRoman(int num) { |
| 106 | + string Roman = ""; |
| 107 | + vector<pair<int, string>> storeIntRoman = {{1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"}, {100, "C"}, {90, "XC"}, {50, "L"}, {40, "XL"}, {10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"}}; |
| 108 | + for (int i = 0; i < storeIntRoman.size(); i++) { |
| 109 | + while (num >= storeIntRoman[i].first) { |
| 110 | + Roman += storeIntRoman[i].second; |
| 111 | + num -= storeIntRoman[i].first; |
| 112 | + } |
| 113 | + } |
| 114 | + return Roman; |
| 115 | + } |
| 116 | +}; |
| 117 | +``` |
| 118 | +
|
| 119 | +#### Java Implementation |
| 120 | +
|
| 121 | +```java |
| 122 | +class Solution { |
| 123 | + public String intToRoman(int num) { |
| 124 | + String Roman = ""; |
| 125 | + int[][] storeIntRoman = {{1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"}, {100, "C"}, {90, "XC"}, {50, "L"}, {40, "XL"}, {10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"}}; |
| 126 | + for (int i = 0; i < storeIntRoman.length; i++) { |
| 127 | + while (num >= storeIntRoman[i][0]) { |
| 128 | + Roman += storeIntRoman[i][1]; |
| 129 | + num -= storeIntRoman[i][0]; |
| 130 | + } |
| 131 | + } |
| 132 | + return Roman; |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +#### Python Implementation |
| 138 | + |
| 139 | +```python |
| 140 | +class Solution: |
| 141 | + def intToRoman(self, num: int) -> str: |
| 142 | + Roman = "" |
| 143 | + storeIntRoman = [[1000, "M"], [900, "CM"], [500, "D"], [400, "CD"], [100, "C"], [90, "XC"], [50, "L"], [40, "XL"], [10, "X"], [9, "IX"], [5, "V"], [4, "IV"], [1, "I"]] |
| 144 | + for i in range(len(storeIntRoman)): |
| 145 | + while num >= storeIntRoman[i][0]: |
| 146 | + Roman += storeIntRoman[i][1] |
| 147 | + num -= storeIntRoman[i][0] |
| 148 | + return Roman |
| 149 | +``` |
| 150 | + |
| 151 | +### Complexity Analysis |
| 152 | + |
| 153 | +- **Time Complexity**: $O(1)$ - The algorithm iterates through a constant number of values (13 in this case), so the time complexity is constant. |
| 154 | +- **Space Complexity**: $O(1)$ - The amount of extra space used is constant, determined by the fixed size of the `storeIntRoman` vector. |
| 155 | + |
| 156 | +## Conclusion |
| 157 | + |
| 158 | +The provided solutions efficiently convert an integer to a Roman numeral by iterating through predefined Roman numeral values and symbols. This approach ensures that the conversion adheres to the rules of the Roman numeral system while maintaining constant time and space complexity. |
| 159 | + |
0 commit comments