Approach: Greedy with Value Table
Map all 13 Roman symbols (including the six subtractive forms) to their values in descending order. Greedily subtract the largest possible value and append the corresponding symbol until the number reaches zero.
function intToRoman(num) {
const values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
const symbols = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I'];
let result = '';
for (let i = 0; i < values.length; i++) {
while (num >= values[i]) {
result += symbols[i];
num -= values[i];
}
}
return result;
}Time Complexity: O(1) — bounded by 13 table entries and at most 15 emitted characters for the allowed input range
Space Complexity: O(1)
Loading discussion…
Saved in this browser only. Private to you.