Advertisement

Integer to Roman - LeetCode 12 Solution

Integer to Roman - Complete Solution Guide

Integer to Roman is LeetCode problem 12, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Seven different symbols represent Roman numerals with the following values: Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 Roman numerals are formed by appending the conversions of decimal place values from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules: If the value does not start with 4 or 9, select the symbol of the maximal value that can be subtracted from the input, append that symbol to the result, subtract its value, and convert the rem

Detailed Explanation

The problem requires converting a given integer (between 1 and 3999) into its Roman numeral representation. Roman numerals use specific symbols (I, V, X, L, C, D, M) to represent numbers. The conversion follows a set of rules involving direct symbol mappings and subtractive notation (e.g., IV for 4, IX for 9). The goal is to produce a string containing the Roman numeral equivalent of the input integer.

Solution Approach

The solution uses a greedy approach with a pre-defined mapping of integer values and their corresponding Roman numeral symbols, including those using subtractive notation. It iterates through this mapping, and for each value, it repeatedly subtracts that value from the input integer and appends the corresponding symbol to the result as long as the input integer is greater than or equal to the value. This ensures the largest possible values are used first.

Step-by-Step Algorithm

  1. Step 1: Initialize a list (or array) `val_sym_map` containing pairs of integer values and their Roman numeral representations. This list is sorted in descending order by the integer values. The list includes pairs for subtractive notation (e.g., (900, "CM")).
  2. Step 2: Initialize an empty string `roman_numeral` to store the result.
  3. Step 3: Iterate through the `val_sym_map` list. For each pair (val, sym):
  4. Step 4: While the input integer `num` is greater than or equal to `val`, append `sym` to the `roman_numeral` string and subtract `val` from `num`.
  5. Step 5: After iterating through all pairs in `val_sym_map`, return the `roman_numeral` string.

Key Insights

  • Insight 1: Recognizing the significance of subtractive notation (IV, IX, XL, XC, CD, CM) is crucial. These combinations avoid repeating a symbol four times.
  • Insight 2: Using a pre-defined mapping of integer values to Roman numeral symbols allows for an efficient iterative approach.
  • Insight 3: Processing the input integer from largest to smallest value ensures the Roman numeral is constructed correctly from left to right.

Complexity Analysis

Time Complexity: O(1)

Space Complexity: O(1)

Topics

This problem involves: Hash Table, Math, String.

Companies

Asked at: Accenture, Adobe, Agoda, Amazon, Apple, Arista Networks, Atlassian, Bloomberg, Booking.com, Citadel, Docusign, Geico, Goldman Sachs, Google, IBM, Infosys, J.P. Morgan, LinkedIn, Meta, Microsoft, NinjaCart, Oracle, Palo Alto Networks, Salesforce, Swiggy, TikTok, Uber, UiPath, VMware, Visa, Wix, X, Yahoo, Zoho, eBay.