Integer to Roman

Key Idea

Solution

class Solution:
    def intToRoman(self, num: int) -> str:
        # Input: num
        # Output: str
        # Integer to Roman
            # if value start with 4 or 9 -> Subractive form
            # if not -> maximal value
            # only powers of 10 can be appeneded consecutively at most 3 times
            # You can not append 5/50 or 500 multiple times, if you need to append 4 times use subtractive form
        # Min 1 Max 3999

        # Initialize
        output_str = ""
        list_num = list(str(num))
        current_size = len(list_num)
        while list_num:
            leftmost_digit = int(list_num.pop(0))
            # Subractive -> Add to the Left
            if leftmost_digit == 4:
                # Hundreds
                if current_size == 3:
                    output_str += "CD" 
                # Tens
                elif current_size == 2:
                    output_str += "XL"
                # Single
                else:
                    output_str += "IV"
            elif leftmost_digit == 9:
                # Hundreds
                if current_size == 3:
                    output_str += "CM" 
                # Tens
                elif current_size == 2:
                    output_str += "XC"
                # Single
                else:
                    output_str += "IX"
            # Addititive -> Add to the Right
            else:
                # Thousands
                if current_size == 4:
                    output_str += "M" * leftmost_digit
                # Hundreds
                elif current_size == 3:
                    if leftmost_digit >= 5:
                        output_str += "D"
                        leftmost_digit -= 5
                    output_str += "C" * leftmost_digit
                # Tens
                elif current_size == 2:
                    if leftmost_digit >= 5:
                        output_str += "L"
                        leftmost_digit -= 5
                    output_str += "X" * leftmost_digit
                # Single
                else:
                    if leftmost_digit >= 5:
                        output_str += "V"
                        leftmost_digit -= 5
                    output_str += "I" * leftmost_digit
            current_size = len(list_num)
        return output_str

Complexity