-
Notifications
You must be signed in to change notification settings - Fork 0
/
2023-12-19 LeetCode 13 - Roman to Integer.js
281 lines (211 loc) · 7.06 KB
/
2023-12-19 LeetCode 13 - Roman to Integer.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// 2023-12-19 Tuesday LeetCode 13 - Roman to Integer
// https://leetcode.com/problems/roman-to-integer/description/
/*
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
Given a roman numeral, convert it to an integer.
Example 1:
Input: s = "III"
Output: 3
Explanation: III = 3.
Example 2:
Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.
Example 3:
Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Constraints:
1 <= s.length <= 15
s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
It is guaranteed that s is a valid roman numeral in the range [1, 3999].
*/
// 2nd attempt
var romanToInt = function(s) {
let integer = 0;
let romanNumerals = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
}
for(let i = 0; i < s.length; i++) {
// Compare current and next numeral to determine whether I just add the value or I subtract current value from next value
if(i < s.length - 1 && romanNumerals[s[i]] < romanNumerals[s[i + 1]]) {
// Current numeral smaller than next; subtract from next
integer += romanNumerals[s[i + 1]] - romanNumerals[s[i]];
// Skip next numeral since I added it
i++;
} else {
integer += romanNumerals[s[i]];
}
}
return integer;
};
// 1st attempt
var romanToInt = function(s) {
let integer = 0;
let romanNumerals = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
}
for(let i = 0; i < s.length; i++) {
if(s[i] >= s[i + 1]) {
for(let number of Object.values(romanNumerals)) {
integer += number;
}
} else {
for(let number of Object.values(romanNumerals)) {
integer += s[i + 1] - s[i];
}
}
}
return integer;
};
/* =============
Other Solutions
============= */
// Fastest code (65ms)
/**
* @param {string} s
* @return {number}
*/
var romanToInt = function(s) {
let res = 0
s = s.split('')
for(let i = 0; i < s.length; i++){
switch(s[i]){
case 'I':
['V','X'].includes(s[i+1]) ? res += -1 : res += 1
break
case 'X':
['L','C'].includes(s[i+1]) ? res += -10 : res += 10
break
case 'C':
['D','M'].includes(s[i+1]) ? res += -100 : res += 100
break
case 'V':
res+=5
break
case 'L':
res += 50
break
case 'D':
res += 500
break
case 'M':
res += 1000
break
}
}
return res
};
// Another 65ms
/**
* @param {string} s
* @return {number}
*/
var romanToInt = function(s) {
const romanVal = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
}
let result = 0;
for(let i=0;i<s.length;i++){
let crtSymVal = romanVal[s[i]];
let nxtSymVal = romanVal[s[i+1]];
if (nxtSymVal > crtSymVal){
result += -crtSymVal;
}else{
result += crtSymVal;
}
}
return result;
};
// Most memory efficient (44.5mb)
var romanToInt = function(s) {
let num = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
};
var answer = 0;
for (let i = s.length - 1; i >= 0; i--) {
const current = num[s[i]];
if (i < s.length - 1 && current < num[s[i + 1]]) {
answer -= current;
} else {
answer += current;
}
}
return answer;
};
// Beat 99.99%
// https://leetcode.com/problems/roman-to-integer/solutions/4127892/beats-99-99-easy-solution-with-o-n-approach-c-python-java-javascript/
/*
✅Intuition
The intuition behind this code is to iterate through the Roman numeral string from right to left, converting each symbol to its corresponding integer value. If the current symbol has a smaller value than the symbol to its right, we subtract its value from the result; otherwise, we add its value to the result. By processing the string from right to left, we can handle cases where subtraction is required (e.g., IV for 4) effectively.
✅Approach for code
Create an unordered map (romanValues) to store the integer values corresponding to each Roman numeral symbol ('I', 'V', 'X', 'L', 'C', 'D', 'M').
Initialize a variable result to 0 to store the accumulated integer value.
Iterate through the input string s from right to left (starting from the last character).
For each character at index i, get its integer value (currValue) from the romanValues map.
Check if the current symbol has a smaller value than the symbol to its right (i.e., currValue < romanValues[s[i + 1]]) using the condition i < s.length() - 1. If true, subtract currValue from the result; otherwise, add it to the result.
Update the result accordingly for each symbol as you iterate through the string.
Finally, return the accumulated result as the integer equivalent of the Roman numeral.
✅Complexity
1. Time complexity:
The time complexity of this code is O(n), where 'n' is the length of the input string s. This is because we iterate through the entire string once from right to left.
2. Space complexity:
The space complexity is O(1) because the size of the romanValues map is fixed and does not depend on the input size. We use a constant amount of additional space to store the result and loop variables, so the space complexity is constant.*/
var romanToInt = function(s) {
const romanValues = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
};
let result = 0;
for (let i = s.length - 1; i >= 0; i--) {
const currValue = romanValues[s[i]];
if (i < s.length - 1 && currValue < romanValues[s[i + 1]]) {
result -= currValue;
} else {
result += currValue;
}
}
return result;
};