This is part of a series of Leetcode solution explanations (index). If you liked this solution or found it useful, please like this post and/or upv...
For further actions, you may consider blocking this person and/or reporting abuse
There is an issue here
as the algo will produce incorrect result when num is multiplied by 2. As Alish Satani (i tried but couldn't mention idk how to :P) commented that this algo produces wrong results on "MDCCCLXXXIV" this roman.
It returns 1664 while the correct answer is 1884. This only happens when num is multiplied by 2.
Dry Run:
Let's try a little dry run (MDCCCLXXXIV):
num = ans = 0;
Starting from right side
We read V: 5 * 2 < 0 (ans initially is 0) = false, so ans becomes 5
Next we read I: 1 * 2 < 5 = true, so we subtract 1 from 5 and ans = 4 now
Next we read X: 10 * 2 < 4 = false, we add 10 to ans, ans = 14
Again we get X: 10 * 2 < 14 = false, we add 10 to ans, ans = 24
Here is the issue we get X again (3rd consecutive): 10 * 2 < 24 "TRUE", now we remove 10 from 24 instead of adding it.
So the trouble begins when the algo encounters same roman element for 3 consecutive times in case we multiply it with 2. You can see this pattern to repeat again in case of DCCCL (3 Cs).
Reason:
Reason for this is as the roman elements can be repeated upto max 3 consecutive times so the sum of all romans on its right side are going to be less then 3 times of the the repeating roman. Like in case of XXX the sum of romans on the right is going to be less then 3 * 10 but when we use 2 as a constant then we don't account for the possibility of 3rd consecutive roman and like in case of XXX 2 * 10 is not less then XXIV (or XX.....).
FIX:
so, the article should say
I haven't seen any example to fail in case of 3 or 4.
Thanks
Thanks, and you're correct. Article has been updated.
Thank you for very detailed explanation.
But i didnt understand why u used 4 in the statement
can u please explain
This part of the explanation covers the 4:
Thank you so much, i got it cleared :)
why is it wrong when I put the test case with roman number :"CMDM"
Because that's not a valid Roman numeral sequence. Roman numbers are written in descending value order, with the exception for 9s and 4s and their equivalents in orders of magnitude.
At the start, the "CM" would be read as 900, because "C" is 100 and "M" is 1000. But then you have a "D", which would be another 500, so "CMD" would be 1400. But you'd never write it that way instead of "MCD" which is 1000+400 (rather than 900+500).
Then you have another "M", which is another 1000, but then that "M" should go at the beginning.
So assuming that the "CM" is supposed to be 900 (rather than 1100), then this should be written as "MMCD", or 1000+1000+400 (M+M+CD).
what do you mean by "with the exception for 9s and 4s and their equivalents in orders of magnitude." I don't understand the "s" meaning and why "9s" and "4s". Sorry, if you have time can you explain to me.
you could check the dev.to/seanpgallivan/solution-inte...
Kind of a one-liner.
It's better to sum in reverse order.
The one-liners are never quite as performant as the more standard code, but I do love one-line solutions!
Suggestions: You can simplify/speed up the solution a bit by condensing the .split() and .map(), while converting to a faster 16-bit typed array with Uint16Array.from(). Then, you can also simplify the .reduce() a bit as well.
This one's written in Rust. Used macro rules for hashmap so that i won't have to insert all the romans and their values one by one into the hashmap.
Thanks for the help, after digging into each method I found the following:
There's no need for:
.into_iter()
since therev()
method returns an interatorand
num
does not need to be mutable in this case.So here would be a little leaner version:
Never Underestimate the POWER OF IF ELSE MUHAHAHAHA
hello there
i have not checked yet the speed, but this accepted
Javascript solution:
var romanToInt = function(s) {
const roman = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
let ans = 0
for(let i = 0; i < s.length; i++){
if(roman[s[i]] < roman[s[i+1]]){
ans -= roman[s[i]]
} else {
ans += (roman[s[i]] || 0) // still accepted using zero or not
}
}
return ans
};
Idea:
Simple Explanation | Java code with detailed comments
Simple Explanation video - youtu.be/iOjKZ4_xQPM
Java code with detailed comments in repository - github.com/prateekgoyal511/dsa/blo...
public static int romanToInt(String s){
int year = 0;
for(int i=0;i<s.length();i++){
char c = s.charAt(i);
if(c == 'I') {
if (i != s.length() - 1 && s.charAt(i + 1) == 'V')
year = year - 1;
else if (i != s.length() - 1 && s.charAt(i + 1) == 'X')
year = year - 1;
else
year = year + 1;
}
else if(c == 'V')
year = year + 5;
else if(c == 'X'){
if (i != s.length() - 1 && s.charAt(i + 1) == 'L')
year = year - 10;
else if (i != s.length() - 1 && s.charAt(i + 1) == 'C')
year = year - 10;
else
year = year + 10;
}
else if(c == 'L')
year = year + 50;
else if(c == 'C'){
if (i != s.length() - 1 && s.charAt(i + 1) == 'D')
year = year - 100;
else if (i != s.length() - 1 && s.charAt(i + 1) == 'M')
year = year - 100;
else
year = year + 100;
}
else if(c =='D')
year = year + 500;
else if(c == 'M')
year = year + 1000;
}
return year;
I did traverse Left to right. Its Accepted .
class Solution {
public int romanToInt(String s) {
Map map=new HashMap<>();
map.put('I',1);
map.put('V',5);
map.put('X',10);
map.put('L',50);
map.put('C',100);
map.put('D',500);
map.put('M',1000);
char [] romanChar=s.toCharArray();
int sum=0;
int i=0;
for(char ch:romanChar){
int flag=1;
if(!(i==s.length()-1) ){
if (map.get(ch) < map.get(s.charAt(i + 1))) {
sum = sum - map.get(ch) ;
flag=0;
}
}
if(flag==1) {
sum = sum + map.get(ch);
}
i++;
}
return sum;
}
}
Very impressive, you have me inspired to write this in Rust. I have actually considered doing this many years ago, but, of course, I never got around to it and forgot about it.
Awesome. Feel free to drop the code in the comments here if you do!!
what do you think of mine one ?
It's wrong for "MDCCCLXXXIV" .
ans should be 1884 and it returns 1664.
Just checked and all four of my codeblocks are properly returning 1884 for that input. Can you copy and paste the code you're using?
Edit: Apparently this happened by using 2 as the multiplier instead of 3 or 4. The article has been updated to correct for this mistake.