I was unable to fully make it by myself, kind of had the same approach but more complicated and less functional, learned a lot from your approach thank you!
Int number = 0; Char[] roman = RomanNum.getChar(); For(int I = 0, i++, i< roman.length()) { Switch roman[i] { Case: "M" NUMBER += 1000; CASE: "V" Number += 5; ... } Return new Integer(number);
this is what I did w/o undoing. for (int i = 0; i < roman.length(); i++) { int romanElement = mapRoman.get(roman.charAt(i)); if (i + 1 < roman.length()) { int nextRomanElement = mapRoman.get(roman.charAt(i + 1)); if (i > 0 && nextRomanElement > romanElement) { result += nextRomanElement - romanElement; i++; } else { result += romanElement; } } else { result += romanElement; } } Thanks so much Kaushik, I just followed your pseudocode. :)
Thank you. this is the simple approach with a good explanation, but you will use less memory and less time if you use arrays, I think the (hashed) map lookup is a bit more expensive in this respect. since we will be using a fixed size small array of constant values/chars. However, this solution is correct.
I feel the right to left would be much better. Here's my solution : int previousDigit = 0; int currentDigit = 0; for(int i=romanNumber.length() - 1; i >= 0; i--) { currentDigit = map.get(romanNumber.charAt(i)); if(currentDigit < previousDigit) { currentDigit = -currentDigit; } decimal += currentDigit; previousDigit = currentDigit; }
I agree with you that this part is not there. But a proper checking mechanism could be seen as a different algorithm, like searching a specific part in the string, so that's probably why he didn't do that edge case. YOu would need to search in whole string cases like you have mentioned and the others too. That would also require new mapping.
I have changed my logic to consider i+1 character while calculating from left to right for (int i = 0; i < no.length(); i++) { int val = romanCharacters.getOrDefault(String.valueOf(no.charAt(i)), 0); if (i + 1 < no.length()) { if (val < romanCharacters.getOrDefault(String.valueOf(no.charAt(i + 1)),0)) { val = val*-1; } } result += val; }
Please upload problems related to dynamic programming and greedy approach and divide and conquer, Huffman coding.. i am grateful to you for this incredible video.
Does this work for XIIV? The Roman numeral for 13? When I run through it in my head I get 15. It doesn’t cover cases where more than one “mistake” was made in a row. Or am I missing something?
Please cover strings , arrays , collections use of construcors, if very strong oops and some concepts , the main idea can be or agenda can be your explanation /easy way can boost confidence to code
You are a genius, very minimal code and great explanation. I suppose each engg. college in India MUST have at least one lecture like you. It is my serious recommendation. Your last name Kothagal always it resmebles Javagal (Srinath bowler)..
Great explanation! I just started studying Java again after over year and this problem had me stuck for days; however I have question, what conditional do we write or how to code for String that does not qualify as roman characters? I guess this is not necessary for problem but in my mind I assumed it was; for example what if someone passed in parameter String s that was simply gibberish such as "HDuihdujhisd9" shouldn't we have a condition for this? I believe this is one aspect that tripped me up on this question for so long and I still do not know how to check for this the most efficient way. I believe there has to be a better way then just writing if (s != X || s!= x ) etc etc.. hope this question makes sense and hope someone can help me 🙏🏾
Why you use charAt() method instead you could have used s[i], after all string is an array of characters right? Or in java it doesn't work that way. can anybody who reads this comment reply me with answer
array of character is not equivalent to String as treated in other programming languages like C and C++. Strings are objects in java hence to get an individual char at specific position charAt() is used.
On leet code they have given - there are only 6 instances of subtraction - IV,IX,XL,XC,CM,CD. This code will cover more than the valid subtractions. Example IM is not a valid roman number, Shouldn't we consider these corner cases? Please help understand.thsnks
An easier way can be to change the if block like this.. if ( i>0 && map.get(s.charAt(i)) < map.get(s.charAt(i+1)) ) { result -= map.get(s.charAt(i)); } it will be easier to understand this code. By the way..Nice explanation!
Please pass some arrays in arguments , pass anonymous objects , you can use a pattern ,lists , maps , to array method, to string , strictly what is majorly used , not vectors exp. Proably help us to code better as how it is exactly used . Say one line thats ok please dont skip a single line as you did not explaining to string in one of your example, Thing is newbie can understand , The same examples will be innovative yet simple but each line of code covered without any skips
other way to solve class Solution { public int romanToInt(String s) { int result = 0;
for (int i = 0; i < s.length(); ++i) { int preChar = (i > 0) ? valueOf(s.charAt(i - 1)) : 0; int curChar = valueOf(s.charAt(i));
if (preChar < curChar) { result = result + curChar - 2 * preChar; } else { result += curChar; } } return result; }
public int valueOf(char c) { switch (c) { case 'I': return 1; case 'V': return 5; case 'X': return 10; case 'L': return 50; case 'C': return 100; case 'D': return 500; case 'M': return 1000; }; return 0; } }
This code works if we assume that the input Roman numeric String is valid It doesn't handle the "invalid case" . For example "CCCD" gives an output of 600 however it's an invalid Roman number . The right equivalent of Number 600 is "CD" and NOT " CCCD"
can you please arrange more problems in sequential manner to go . We dont want core java and wanna learn with you same way solving prolems, Anyways in projects we use anonymous objects passing, singeltons and factory , Just basicstuff is all which all teach , BASic----> Advance gradually nobody takes
I was unable to fully make it by myself, kind of had the same approach but more complicated and less functional, learned a lot from your approach thank you!
CCXLVIII = 248 u have shown CCXLVII i.e 247 however that doesn't affect anything but thanks keep making this kind of problem solving videos
Honestly this is the first time I've understood this problem after watching this video. Great explanation!
Int number = 0;
Char[] roman = RomanNum.getChar();
For(int I = 0, i++, i< roman.length())
{
Switch roman[i] {
Case: "M"
NUMBER += 1000;
CASE: "V"
Number += 5;
... }
Return new Integer(number);
it wont work for subtractive use case, will it???
this is what I did w/o undoing.
for (int i = 0; i < roman.length(); i++) {
int romanElement = mapRoman.get(roman.charAt(i));
if (i + 1 < roman.length()) {
int nextRomanElement = mapRoman.get(roman.charAt(i + 1));
if (i > 0 && nextRomanElement > romanElement) {
result += nextRomanElement - romanElement;
i++;
} else {
result += romanElement;
}
} else {
result += romanElement;
}
}
Thanks so much Kaushik, I just followed your pseudocode. :)
You start recording more question like this and solve them will help us get different approach .
thanks
LOVE THIS CHANNEL
Really thanks man, for this so crystal clear explanation.... my first video forced ....me to hit subscribe...thanks again...
Thank you. this is the simple approach with a good explanation, but you will use less memory and less time if you use arrays, I think the (hashed) map lookup is a bit more expensive in this respect. since we will be using a fixed size small array of constant values/chars. However, this solution is correct.
Great Explanation!! Thanks
Loving these problem solving videos. Keep 'em coming :D
Very good explanation, to the point. Thank you.
Great explanation. Thank you!👍
Love this channel!
Awesome !!!
🔥🔥🔥
Sir please make a video on how to get all permutations of a string.
Please solve it without recursion.
Very nicely, Explained!
Thank you!
Really nice way of thinking. I was able to do it with if statements only. But your way is a lot better. thanks!
how did you do it with if statements only?
This made me take more interest in programming
Well explained!
Approach is very good
Wow I like the way you explained it.
Bro I need the 150 interview questions on leetcode from you , Man great explaination
Doesn't he sounds like Rohit from Koi Mil Gaya...Aaila Jaadu
Very succinct explanation. Great Job
Superb!
I feel the right to left would be much better. Here's my solution :
int previousDigit = 0;
int currentDigit = 0;
for(int i=romanNumber.length() - 1; i >= 0; i--) {
currentDigit = map.get(romanNumber.charAt(i));
if(currentDigit < previousDigit) {
currentDigit = -currentDigit;
}
decimal += currentDigit;
previousDigit = currentDigit;
}
How to check for invalid scemarios like IIII or VV ?
I agree with you that this part is not there. But a proper checking mechanism could be seen as a different algorithm, like searching a specific part in the string, so that's probably why he didn't do that edge case. YOu would need to search in whole string cases like you have mentioned and the others too. That would also require new mapping.
I have changed my logic to consider i+1 character while calculating from left to right
for (int i = 0; i < no.length(); i++) {
int val = romanCharacters.getOrDefault(String.valueOf(no.charAt(i)), 0);
if (i + 1 < no.length()) {
if (val < romanCharacters.getOrDefault(String.valueOf(no.charAt(i + 1)),0)) {
val = val*-1;
}
}
result += val;
}
Please upload problems related to dynamic programming and greedy approach and divide and conquer, Huffman coding.. i am grateful to you for this incredible video.
thank you very much sir for this video
Nice explanation. Thanks you...sir
Thanks, clear explain!
Thanks sir
Please do a video about remember me option with session expire problem. You are an awesome teacher.
Thank you Sir :)
thank you for the explaination
The algorithm is good, but the conversion is not correct. Funny, but that Roman is 247, not 248 number. Or is it click bait?
Haha, thanks for letting me know. It was actually a genuine mistake, but if it acts as clickbait, I'll take it! Anyway, I've fixed it now.
why not start from i=1 so we do not have to check if i>0 in the if clause?
Please continue for some more questions. It's really appreciate your efforts
one thing while Testing XXL -getting 50 ideal it should be 30 right ?
it does not pass all the test cases in leet code. but thanks for showing the approach
good
We can use simple switch case to solve this problem where we dont need hashmap to store the roman values.
For this space complexity will be O(1) right, as you are storing only 7 values in the map for complete program....?
my solution:
public class Test {
public static void main(String[] args) {
String input = getInput();
int output = processInput(input);
System.out.println(output);
}
private static int processInput(String input) {
int sum = 0;
char[] charArray = input.toCharArray();
for(int i=0; i < input.length(); i++) {
if(i > 0) {
int previousValue = getEquivalent(charArray[i-1]);
if(previousValue < getEquivalent(charArray[i]))
sum -= (previousValue*2);
}
sum += getEquivalent(charArray[i]);
}
return sum;
}
private static int getEquivalent(char romans) {
switch(romans) {
case 'C' : return 100;
case 'L' : return 50;
case 'X' : return 10;
case 'V' : return 5;
case 'I' : return 1;
}
return 0;
}
public static String getInput() {
System.out.print("Enter a roman: ");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
scanner.close();
return input;
}
}
CCXLVII is 247
When you make video on spring boot with ouath2 integrated with api gateway.
Does this work for XIIV? The Roman numeral for 13?
When I run through it in my head I get 15. It doesn’t cover cases where more than one “mistake” was made in a row. Or am I missing something?
I ran XIIV in leetcode it said this string is not a valid roman integer.
Please cover strings , arrays , collections use of construcors, if very strong oops and some concepts , the main idea can be or agenda can be your explanation /easy way can boost confidence to code
You are a genius, very minimal code and great explanation. I suppose each engg. college in India MUST have at least one lecture like you. It is my serious recommendation. Your last name Kothagal always it resmebles Javagal (Srinath bowler)..
the zero exists in roman number!! now it's "" ;)
Zero is place holder number, not actual value here
Traversing the string from right to left seems to be better as you dont need to course correct yourself.
Great explanation! I just started studying Java again after over year and this problem had me stuck for days; however I have question, what conditional do we write or how to code for String that does not qualify as roman characters? I guess this is not necessary for problem but in my mind I assumed it was; for example what if someone passed in parameter String s that was simply gibberish such as "HDuihdujhisd9" shouldn't we have a condition for this? I believe this is one aspect that tripped me up on this question for so long and I still do not know how to check for this the most efficient way. I believe there has to be a better way then just writing if (s != X || s!= x ) etc etc.. hope this question makes sense and hope someone can help me 🙏🏾
Why you use charAt() method instead you could have used s[i], after all string is an array of characters right? Or in java it doesn't work that way. can anybody who reads this comment reply me with answer
array of character is not equivalent to String as treated in other programming languages like C and C++. Strings are objects in java hence to get an individual char at specific position charAt() is used.
On leet code they have given - there are only 6 instances of subtraction - IV,IX,XL,XC,CM,CD.
This code will cover more than the valid subtractions.
Example IM is not a valid roman number,
Shouldn't we consider these corner cases? Please help understand.thsnks
such number will not be provided, so you dont have to worry about such case. you got correct logic tho
An easier way can be to change the if block like this..
if ( i>0 && map.get(s.charAt(i)) < map.get(s.charAt(i+1)) ) {
result -= map.get(s.charAt(i));
}
it will be easier to understand this code.
By the way..Nice explanation!
When i = s.length() - 1 in the iteration, fetching i+1 character from s will throw exception..
@@chethan93 we can check that i is in between 0 and the last index too in the loop
Please pass some arrays in arguments , pass anonymous objects , you can use a pattern ,lists , maps , to array method, to string , strictly what is majorly used , not vectors exp. Proably help us to code better as how it is exactly used . Say one line thats ok please dont skip a single line as you did not explaining to string in one of your example, Thing is newbie can understand , The same examples will be innovative yet simple but each line of code covered without any skips
In this line of code, if (i > 0 && s.charAt(i) > s.charAt(i - 1)), why do we need i > 0? I'm confused.
Coz in roman letters there is no concept of 0 so it has to be always >0. If you look at the entire hashmap there is no zero entry ->map.put(" ",0)
The i is your index into the String. If i=0, what happens with the charAt 0-1? It would be an error.
My brain liked this more ....
for(int i =0; i
Isn't it 248 should be CCXLVIII ??
yes?
other way to solve
class Solution {
public int romanToInt(String s) {
int result = 0;
for (int i = 0; i < s.length(); ++i) {
int preChar = (i > 0) ? valueOf(s.charAt(i - 1)) : 0;
int curChar = valueOf(s.charAt(i));
if (preChar < curChar) {
result = result + curChar - 2 * preChar;
} else {
result += curChar;
}
}
return result;
}
public int valueOf(char c) {
switch (c) {
case 'I':
return 1;
case 'V':
return 5;
case 'X':
return 10;
case 'L':
return 50;
case 'C':
return 100;
case 'D':
return 500;
case 'M':
return 1000;
};
return 0;
}
}
you have to add more logic on 5 ,99 and character repeated more than 3 times
why is this not giving me correct result?:(((((
it does, works fine for me on eclipse
Hi koushik please continue with spring security oauth2 with roles..
Yes, an OAuth explainer coming this Friday/Saturday. Will continue the series after that.
Core java please
Only a programmer would laugh at the “charAt” reference 😄
What's the problem with charAt🙄
@@nishant5249 He pronounced it as Carrot and then corrected is a CharAt and laughed about it. 5:00
Thank you
You worked for 247 not 248
I think you're a brilliant Engineer but you need to rehearse more on your script...
This code works if we assume that the input Roman numeric String is valid
It doesn't handle the "invalid case" . For example "CCCD" gives an output of 600 however it's an invalid Roman number . The right equivalent of Number 600 is "CD" and NOT " CCCD"
its "DC"
can you please arrange more problems in sequential manner to go . We dont want core java and wanna learn with you same way solving prolems, Anyways in projects we use anonymous objects passing, singeltons and factory , Just basicstuff is all which all teach , BASic----> Advance gradually nobody takes
thank you bro. more spring cloud please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please, please.
if you dont like his face but like the knowledge jump to 4:31 , thanks me later
10 min ka video bane ke liya starting 4 min bakwas kar diya.....
Everyone start watch after 4 min