Convert Roman numeral to Integer - LeetCode Interview Coding Challenge [Java Brains]

  Рет қаралды 73,152

Java Brains

Java Brains

Күн бұрын

Пікірлер: 100
@SuperYouthful
@SuperYouthful 4 жыл бұрын
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);
@amitgautam6019
@amitgautam6019 3 жыл бұрын
it wont work for subtractive use case, will it???
@centurion8158
@centurion8158 4 жыл бұрын
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!
@lalitvavdara5841
@lalitvavdara5841 4 жыл бұрын
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
@arpit39agarwal
@arpit39agarwal 4 жыл бұрын
You start recording more question like this and solve them will help us get different approach . thanks
@caseycampbell8047
@caseycampbell8047 2 жыл бұрын
Honestly this is the first time I've understood this problem after watching this video. Great explanation!
@DougieMuringani
@DougieMuringani 4 жыл бұрын
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.
@adeshgaikwad2192
@adeshgaikwad2192 4 жыл бұрын
Doesn't he sounds like Rohit from Koi Mil Gaya...Aaila Jaadu
@vishal24000
@vishal24000 5 жыл бұрын
Loving these problem solving videos. Keep 'em coming :D
@rajeshpaithari7320
@rajeshpaithari7320 9 ай бұрын
Great Explanation!! Thanks
@SomeoneAlive90
@SomeoneAlive90 2 жыл бұрын
Love this channel!
@jnayehsirine6222
@jnayehsirine6222 Жыл бұрын
LOVE THIS CHANNEL
@ritikbhardwaj4061
@ritikbhardwaj4061 4 жыл бұрын
Really thanks man, for this so crystal clear explanation.... my first video forced ....me to hit subscribe...thanks again...
@monsterhuntergo
@monsterhuntergo 2 жыл бұрын
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. :)
@shubhamsaurabh4419
@shubhamsaurabh4419 5 жыл бұрын
Sir please make a video on how to get all permutations of a string. Please solve it without recursion.
@abhijitmadchetti5443
@abhijitmadchetti5443 4 жыл бұрын
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; }
@GagandeepSingh-lz5bg
@GagandeepSingh-lz5bg 3 жыл бұрын
Very good explanation, to the point. Thank you.
@akshayborse5756
@akshayborse5756 3 жыл бұрын
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; }
@namratam1522
@namratam1522 5 жыл бұрын
This made me take more interest in programming
@ShaliniNegi24
@ShaliniNegi24 4 жыл бұрын
Very nicely, Explained! Thank you!
@evgenii-govorushkin
@evgenii-govorushkin Жыл бұрын
Great explanation. Thank you!👍
@Ariesgod1998
@Ariesgod1998 Жыл бұрын
Bro I need the 150 interview questions on leetcode from you , Man great explaination
@vidhi_bhatt
@vidhi_bhatt 2 жыл бұрын
Well explained!
@MaggicBones
@MaggicBones 3 жыл бұрын
Awesome !!!
@namratam1522
@namratam1522 5 жыл бұрын
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.
@neludonellyakejellu1914
@neludonellyakejellu1914 4 жыл бұрын
Very succinct explanation. Great Job
@deb5976
@deb5976 2 жыл бұрын
Really nice way of thinking. I was able to do it with if statements only. But your way is a lot better. thanks!
@KrishNamaste
@KrishNamaste Жыл бұрын
how did you do it with if statements only?
@kibizoid
@kibizoid 5 жыл бұрын
The algorithm is good, but the conversion is not correct. Funny, but that Roman is 247, not 248 number. Or is it click bait?
@Java.Brains
@Java.Brains 5 жыл бұрын
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.
@continnum_radhe-radhe
@continnum_radhe-radhe 2 жыл бұрын
🔥🔥🔥
@saddamahmad2310
@saddamahmad2310 5 жыл бұрын
thank you very much sir for this video
@sagnikghoshcr7
@sagnikghoshcr7 4 жыл бұрын
Approach is very good
@SurendraBabuK
@SurendraBabuK 5 жыл бұрын
Please continue for some more questions. It's really appreciate your efforts
@SR-we1vl
@SR-we1vl 4 жыл бұрын
Superb!
@maarif1869
@maarif1869 5 жыл бұрын
Please do a video about remember me option with session expire problem. You are an awesome teacher.
@YazhShah
@YazhShah 3 жыл бұрын
Wow I like the way you explained it.
@keshavdeosharma7222
@keshavdeosharma7222 10 ай бұрын
We can use simple switch case to solve this problem where we dont need hashmap to store the roman values.
@dattukhambam9176
@dattukhambam9176 4 жыл бұрын
Nice explanation. Thanks you...sir
@ZhouHaibo
@ZhouHaibo 3 жыл бұрын
Thanks, clear explain!
@kingop2679
@kingop2679 Ай бұрын
Thanks sir
@dev-skills
@dev-skills 3 жыл бұрын
Traversing the string from right to left seems to be better as you dont need to course correct yourself.
@learntech3982
@learntech3982 5 жыл бұрын
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
@carlestutusausmarrugat8889
@carlestutusausmarrugat8889 5 жыл бұрын
the zero exists in roman number!! now it's "" ;)
@venkatp968
@venkatp968 4 жыл бұрын
Zero is place holder number, not actual value here
@ajeetworking
@ajeetworking 4 жыл бұрын
thank you for the explaination
@RaghuDevBattula
@RaghuDevBattula Жыл бұрын
Thank you Sir :)
@anonymous_99889
@anonymous_99889 3 жыл бұрын
good
@janellogrono7989
@janellogrono7989 3 жыл бұрын
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; } }
@amanroy8447
@amanroy8447 5 жыл бұрын
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!
@chethan93
@chethan93 4 жыл бұрын
When i = s.length() - 1 in the iteration, fetching i+1 character from s will throw exception..
@arijeetbhattacharya9787
@arijeetbhattacharya9787 4 жыл бұрын
@@chethan93 we can check that i is in between 0 and the last index too in the loop
@sharikkumar8581
@sharikkumar8581 4 жыл бұрын
it does not pass all the test cases in leet code. but thanks for showing the approach
@aravindrajesh4484
@aravindrajesh4484 3 жыл бұрын
CCXLVII is 247
@learntech3982
@learntech3982 5 жыл бұрын
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
@hey-cg3fv
@hey-cg3fv 2 жыл бұрын
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; } }
@cjm101
@cjm101 2 жыл бұрын
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 🙏🏾
@roushanraj8530
@roushanraj8530 3 жыл бұрын
For this space complexity will be O(1) right, as you are storing only 7 values in the map for complete program....?
@satishthakur2156
@satishthakur2156 Жыл бұрын
one thing while Testing XXL -getting 50 ideal it should be 30 right ?
@nrakshi3060
@nrakshi3060 4 жыл бұрын
How to check for invalid scemarios like IIII or VV ?
@JohannGambolputty86
@JohannGambolputty86 3 жыл бұрын
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.
@sysybaba420
@sysybaba420 Жыл бұрын
why not start from i=1 so we do not have to check if i>0 in the if clause?
@akshaykumar-uv3up
@akshaykumar-uv3up 5 жыл бұрын
When you make video on spring boot with ouath2 integrated with api gateway.
@arian.mohajer
@arian.mohajer 2 жыл бұрын
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?
@NancyC8
@NancyC8 2 жыл бұрын
I ran XIIV in leetcode it said this string is not a valid roman integer.
@sangharshadhital9610
@sangharshadhital9610 Жыл бұрын
you have to add more logic on 5 ,99 and character repeated more than 3 times
@venkatagirivatlam9624
@venkatagirivatlam9624 3 жыл бұрын
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)..
@sinsere-3615
@sinsere-3615 3 жыл бұрын
My brain liked this more .... for(int i =0; i
@Seemakrid
@Seemakrid 4 жыл бұрын
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
@kombophoto
@kombophoto 3 жыл бұрын
such number will not be provided, so you dont have to worry about such case. you got correct logic tho
@TheCasualGamer06
@TheCasualGamer06 4 жыл бұрын
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
@amitgautam6019
@amitgautam6019 3 жыл бұрын
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.
@narendramuppala4448
@narendramuppala4448 4 жыл бұрын
Only a programmer would laugh at the “charAt” reference 😄
@nishant5249
@nishant5249 4 жыл бұрын
What's the problem with charAt🙄
@narendramuppala4448
@narendramuppala4448 4 жыл бұрын
@@nishant5249 He pronounced it as Carrot and then corrected is a CharAt and laughed about it. 5:00
@cvxcfv
@cvxcfv 3 жыл бұрын
I think you're a brilliant Engineer but you need to rehearse more on your script...
@johnsondurant9905
@johnsondurant9905 4 жыл бұрын
In this line of code, if (i > 0 && s.charAt(i) > s.charAt(i - 1)), why do we need i > 0? I'm confused.
@nribackpacker
@nribackpacker 4 жыл бұрын
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)
@benfowlie5147
@benfowlie5147 4 жыл бұрын
The i is your index into the String. If i=0, what happens with the charAt 0-1? It would be an error.
@learntech3982
@learntech3982 5 жыл бұрын
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
@mdsiddiq4145
@mdsiddiq4145 5 жыл бұрын
Hi koushik please continue with spring security oauth2 with roles..
@Java.Brains
@Java.Brains 5 жыл бұрын
Yes, an OAuth explainer coming this Friday/Saturday. Will continue the series after that.
@yadneshkhode3091
@yadneshkhode3091 5 жыл бұрын
Thank you
@shivisaxena2798
@shivisaxena2798 2 жыл бұрын
You worked for 247 not 248
@bhavinmajethia7070
@bhavinmajethia7070 3 жыл бұрын
Isn't it 248 should be CCXLVIII ??
@mateuszperyt7998
@mateuszperyt7998 3 жыл бұрын
yes?
@mirandahobbes8189
@mirandahobbes8189 3 жыл бұрын
why is this not giving me correct result?:(((((
@mateuszperyt7998
@mateuszperyt7998 3 жыл бұрын
it does, works fine for me on eclipse
@sharathchandrareddy8959
@sharathchandrareddy8959 4 жыл бұрын
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"
@santoshjackman
@santoshjackman 2 жыл бұрын
its "DC"
@aradhanasinghal138
@aradhanasinghal138 5 жыл бұрын
Core java please
@DT-oo5mp
@DT-oo5mp 5 жыл бұрын
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.
@shrad6611
@shrad6611 2 жыл бұрын
if you dont like his face but like the knowledge jump to 4:31 , thanks me later
@itsourav
@itsourav 3 жыл бұрын
10 min ka video bane ke liya starting 4 min bakwas kar diya..... Everyone start watch after 4 min
БУ, ИСПУГАЛСЯ?? #shorts
00:22
Паша Осадчий
Рет қаралды 3 МЛН
Roman to Integer - Leetcode 13 - Python
7:57
NeetCode
Рет қаралды 179 М.
How I Failed the Google Coding Interview (and lessons I learned)
14:24
Detect Capital - LeetCode Interview Coding Challenge [Java Brains]
25:09
Integer to Roman - Leetcode 12 - Python
9:43
NeetCode
Рет қаралды 77 М.
How Senior Programmers ACTUALLY Write Code
13:37
Thriving Technologist
Рет қаралды 1,6 МЛН
5 interview techniques to show problem solving skills
17:23
Java Brains
Рет қаралды 11 М.
Object-Oriented Programming is Embarrassing: 4 Short Examples
28:03
Brian Will
Рет қаралды 2,1 МЛН
Array vs. ArrayList in Java Tutorial - What's The Difference?
17:36
Coding with John
Рет қаралды 555 М.