-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12_integer-to-roman.ts
More file actions
73 lines (65 loc) · 1.41 KB
/
12_integer-to-roman.ts
File metadata and controls
73 lines (65 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* ⭐️ 문제 정보 ⭐️
* 문제 : 12 - Integer to Roman
* 레벨 : Medium
* 링크 : https://leetcode.com/problems/integer-to-roman/
*/
// greedy
function intToRoman1(num: number): string {
const symbols = [
"M",
"CM",
"D",
"CD",
"C",
"XC",
"L",
"XL",
"X",
"IX",
"V",
"IV",
"I",
];
const values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
let answer: string[] = [];
for (let idx = 0; idx < values.length; idx++) {
const value = values[idx];
const cnt = Math.floor(num / value);
if (cnt <= 0) continue;
const symbol = symbols[idx];
answer.push(symbol.repeat(cnt));
num %= value;
}
return answer.join("");
}
// map과 iterator를 이용해서 비교적 덜 명료했던 첫번째 코드
function intToRoman2(num: number): string {
const symbolTable = new Map([
[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"],
]);
const valueIter = symbolTable.keys();
let answer: string[] = [];
let currValue = valueIter.next().value;
while (num > 0) {
while (currValue > num) {
currValue = valueIter.next().value;
}
const currSymbol = symbolTable.get(currValue)!;
answer.push(currSymbol);
num -= currValue;
}
return answer.join("");
}