DEV Community

Cover image for LeetCode Challenge: 12. Integer to Roman - JavaScript Solution πŸš€
Rahul Kumar Barnwal
Rahul Kumar Barnwal

Posted on

LeetCode Challenge: 12. Integer to Roman - JavaScript Solution πŸš€

Top Interview 150

Converting integers to Roman numerals is a fun problem involving string manipulation and careful application of Roman numeral rules. Let’s explore LeetCode 12: Integer to Roman, break down the steps, and implement the solution in JavaScript.


πŸš€ Problem Description

You are given an integer num (1 ≀ num ≀ 3999).
Return its Roman numeral representation.

Roman numerals follow specific rules:

  • Symbols: I (1), V (5), X (10), L (50), C (100), D (500), M (1000).
  • Use subtractive notation for 4, 9, 40, 90, 400, 900.

πŸ’‘ Examples

Example 1

Input: num = 3749 Output: "MMMDCCXLIX" Explanation: - 3000 β†’ `MMM` - 700 β†’ `DCC` - 40 β†’ `XL` - 9 β†’ `IX` 
Enter fullscreen mode Exit fullscreen mode

Example 2

Input: num = 58 Output: "LVIII" Explanation: - 50 β†’ `L` - 8 β†’ `VIII` 
Enter fullscreen mode Exit fullscreen mode

Example 3

Input: num = 1994 Output: "MCMXCIV" Explanation: - 1000 β†’ `M` - 900 β†’ `CM` - 90 β†’ `XC` - 4 β†’ `IV` 
Enter fullscreen mode Exit fullscreen mode

πŸ† JavaScript Solution

To solve this problem, we’ll use a greedy approach:

  • Start with the largest Roman numeral.
  • Subtract its value from the number while appending the numeral to the result.
  • Repeat until the number becomes zero.

Implementation

var intToRoman = function(num) { const romanMap = [ { value: 1000, symbol: 'M' }, { value: 900, symbol: 'CM' }, { value: 500, symbol: 'D' }, { value: 400, symbol: 'CD' }, { value: 100, symbol: 'C' }, { value: 90, symbol: 'XC' }, { value: 50, symbol: 'L' }, { value: 40, symbol: 'XL' }, { value: 10, symbol: 'X' }, { value: 9, symbol: 'IX' }, { value: 5, symbol: 'V' }, { value: 4, symbol: 'IV' }, { value: 1, symbol: 'I' }, ]; let result = ''; for (const { value, symbol } of romanMap) { while (num >= value) { result += symbol; num -= value; } } return result; }; 
Enter fullscreen mode Exit fullscreen mode

πŸ” How It Works

  1. Roman Map:

    • Create an ordered list of Roman numeral values and their symbols.
    • Include subtractive notations (CM, XC, etc.) in the map.
  2. Iterate Through the Map:

    • For each numeral, append it to the result while subtracting its value from num until num is less than the numeral’s value.
  3. Return the Result:

    • After processing all numerals, the result contains the Roman numeral string.

πŸ”‘ Complexity Analysis

  • > Time Complexity: O(1), because the number of Roman numeral symbols is constant (13). The loop runs a fixed number of iterations regardless of input size.
  • > Space Complexity: O(1), as we only use a string to build the result.

πŸ“‹ Dry Run

Input: num = 1994
Roman to Integer
Output: "MCMXCIV"


✨ Pro Tips for Interviews

  1. Use a Roman map: Predefining values simplifies the algorithm.
  2. Think about scalability: This solution is efficient since the Roman numeral system has fixed rules and symbols.
  3. Explain constraints: Confirm the input range (1 to 3999) to clarify assumptions.

πŸ“š Learn More

Check out the full explanation and code walkthrough on my Dev.to post:
πŸ‘‰ Roman to Integer - JavaScript Solution

How would you optimize this further? Let’s discuss below! πŸš€

JavaScript #LeetCode #CodingInterview #ProblemSolving

Top comments (1)

Collapse
 
rahulgithubweb profile image
Rahul Kumar Barnwal

Follow Me on GitHub πŸš€

If you found this solution helpful, check out more of my projects and solutions on my GitHub profile.

Don't forget to follow for more updates!