diff --git a/clojure/12.clj b/clojure/12.clj deleted file mode 100644 index 8933337..0000000 --- a/clojure/12.clj +++ /dev/null @@ -1,45 +0,0 @@ -(ns clojure.12 - "Leetcode: 12\n - Name: Integer to Roman\n" - (:require [clojure.test :refer [deftest is run-tests]])) - -(defonce roman-symbols - [["M" 1000] ["CM" 900] ["D" 500] ["CD" 400] ["C" 100] ["XC" 90] - ["L" 50] ["XL" 40] ["X" 10] ["IX" 9] ["V" 5] ["IV" 4] ["I" 1]]) - -(defn int-to-roman - "Convert an integer to its Roman numeral representation. - - Args: - num - An integer value to convert (1 <= num <= 3999). - - Returns: - A string representing the Roman numeral equivalent of the integer. - - Examples: - (int-to-roman 4) ;=> \"IV\" - (int-to-roman 9) ;=> \"IX\" - (int-to-roman 1994) ;=> \"MCMXCIV\"" - [num] - {:pre [(and (integer? num) (<= 1 num 3999))]} - (loop [number num - result ""] - (if (zero? number) - result - (let [[symbol value] (first (filter #(<= (second %) number) roman-symbols))] - (recur (- number value) - (str result symbol)))))) - -(deftest test-int-to-roman - (is (= (int-to-roman 1) "I")) - (is (= (int-to-roman 4) "IV")) - (is (= (int-to-roman 9) "IX")) - (is (= (int-to-roman 58) "LVIII")) - (is (= (int-to-roman 1994) "MCMXCIV")) - (is (= (int-to-roman 3999) "MMMCMXCIX")) - (is (= (int-to-roman 1) "I")) - (is (= (int-to-roman 3999) "MMMCMXCIX")) - (is (thrown? AssertionError (int-to-roman 0))) - (is (thrown? AssertionError (int-to-roman 4000)))) - -(run-tests) diff --git a/solutions/0001-0100/0012-integer-to-roman/README.md b/solutions/0001-0100/0012-integer-to-roman/README.md new file mode 100644 index 0000000..f639646 --- /dev/null +++ b/solutions/0001-0100/0012-integer-to-roman/README.md @@ -0,0 +1,12 @@ +# 12. Integer to Roman + +[LeetCode problem 12](https://leetcode.com/problems/integer-to-roman/) + +- **Difficulty**: Medium +- **Tags**: hash-table, math, string, greedy + +## Description + +Given an integer in the range `[1, 3999]`, convert it to a Roman numeral, +handling the six subtractive pairs (`IV`, `IX`, `XL`, `XC`, `CD`, `CM`) that +Roman numerals use in place of four repeated symbols in a row. diff --git a/solutions/0001-0100/0012-integer-to-roman/solution.cpp b/solutions/0001-0100/0012-integer-to-roman/solution.cpp new file mode 100644 index 0000000..49bb2aa --- /dev/null +++ b/solutions/0001-0100/0012-integer-to-roman/solution.cpp @@ -0,0 +1,37 @@ +#include "solution.hpp" + +#include +#include + +namespace leetcode::p0012 { + +std::string solve(int num) { + static constexpr std::array, 13> romanSymbols{{ + {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"}, + }}; + + std::string result; + + for (const auto& [value, symbol] : romanSymbols) { + while (num >= value) { + result += symbol; + num -= value; + } + } + + return result; +} + +} // namespace leetcode::p0012 diff --git a/solutions/0001-0100/0012-integer-to-roman/solution.hpp b/solutions/0001-0100/0012-integer-to-roman/solution.hpp new file mode 100644 index 0000000..cc2f7a2 --- /dev/null +++ b/solutions/0001-0100/0012-integer-to-roman/solution.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace leetcode::p0012 { + +// optimized: O(1) time (13 fixed symbols), O(1) space — greedy, largest +// symbol first. +std::string solve(int num); + +} // namespace leetcode::p0012 diff --git a/solutions/0001-0100/0012-integer-to-roman/test.cpp b/solutions/0001-0100/0012-integer-to-roman/test.cpp new file mode 100644 index 0000000..c5f789d --- /dev/null +++ b/solutions/0001-0100/0012-integer-to-roman/test.cpp @@ -0,0 +1,12 @@ +#include + +#include "solution.hpp" + +TEST_CASE("Problem 12", "[p0012]") { + CHECK(leetcode::p0012::solve(1) == "I"); + CHECK(leetcode::p0012::solve(4) == "IV"); + CHECK(leetcode::p0012::solve(9) == "IX"); + CHECK(leetcode::p0012::solve(58) == "LVIII"); + CHECK(leetcode::p0012::solve(1994) == "MCMXCIV"); + CHECK(leetcode::p0012::solve(3999) == "MMMCMXCIX"); +} diff --git a/swift/12.swift b/swift/12.swift deleted file mode 100644 index 59f6cfb..0000000 --- a/swift/12.swift +++ /dev/null @@ -1,31 +0,0 @@ -class Solution { - func intToRoman(_ num: Int) -> String { - let romanSymbols = [ - ("M", 1000), - ("CM", 900), - ("D", 500), - ("CD", 400), - ("C", 100), - ("XC", 90), - ("L", 50), - ("XL", 40), - ("X", 10), - ("IX", 9), - ("V", 5), - ("IV", 4), - ("I", 1) - ] - - var number = num - var result = "" - - for (symbol, value) in romanSymbols { - while number >= value { - result += symbol - number -= value - } - } - - return result - } -}