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);
@amitgautam60193 жыл бұрын
it wont work for subtractive use case, will it???
@centurion81584 жыл бұрын
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!
@lalitvavdara58414 жыл бұрын
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
@arpit39agarwal4 жыл бұрын
You start recording more question like this and solve them will help us get different approach . thanks
@caseycampbell80472 жыл бұрын
Honestly this is the first time I've understood this problem after watching this video. Great explanation!
@DougieMuringani4 жыл бұрын
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.
@adeshgaikwad21924 жыл бұрын
Doesn't he sounds like Rohit from Koi Mil Gaya...Aaila Jaadu
@vishal240005 жыл бұрын
Loving these problem solving videos. Keep 'em coming :D
@rajeshpaithari73209 ай бұрын
Great Explanation!! Thanks
@SomeoneAlive902 жыл бұрын
Love this channel!
@jnayehsirine6222 Жыл бұрын
LOVE THIS CHANNEL
@ritikbhardwaj40614 жыл бұрын
Really thanks man, for this so crystal clear explanation.... my first video forced ....me to hit subscribe...thanks again...
@monsterhuntergo2 жыл бұрын
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. :)
@shubhamsaurabh44195 жыл бұрын
Sir please make a video on how to get all permutations of a string. Please solve it without recursion.
@abhijitmadchetti54434 жыл бұрын
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-lz5bg3 жыл бұрын
Very good explanation, to the point. Thank you.
@akshayborse57563 жыл бұрын
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; }
@namratam15225 жыл бұрын
This made me take more interest in programming
@ShaliniNegi244 жыл бұрын
Very nicely, Explained! Thank you!
@evgenii-govorushkin Жыл бұрын
Great explanation. Thank you!👍
@Ariesgod1998 Жыл бұрын
Bro I need the 150 interview questions on leetcode from you , Man great explaination
@vidhi_bhatt2 жыл бұрын
Well explained!
@MaggicBones3 жыл бұрын
Awesome !!!
@namratam15225 жыл бұрын
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.
@neludonellyakejellu19144 жыл бұрын
Very succinct explanation. Great Job
@deb59762 жыл бұрын
Really nice way of thinking. I was able to do it with if statements only. But your way is a lot better. thanks!
@KrishNamaste Жыл бұрын
how did you do it with if statements only?
@kibizoid5 жыл бұрын
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.Brains5 жыл бұрын
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-radhe2 жыл бұрын
🔥🔥🔥
@saddamahmad23105 жыл бұрын
thank you very much sir for this video
@sagnikghoshcr74 жыл бұрын
Approach is very good
@SurendraBabuK5 жыл бұрын
Please continue for some more questions. It's really appreciate your efforts
@SR-we1vl4 жыл бұрын
Superb!
@maarif18695 жыл бұрын
Please do a video about remember me option with session expire problem. You are an awesome teacher.
@YazhShah3 жыл бұрын
Wow I like the way you explained it.
@keshavdeosharma722210 ай бұрын
We can use simple switch case to solve this problem where we dont need hashmap to store the roman values.
@dattukhambam91764 жыл бұрын
Nice explanation. Thanks you...sir
@ZhouHaibo3 жыл бұрын
Thanks, clear explain!
@kingop2679Ай бұрын
Thanks sir
@dev-skills3 жыл бұрын
Traversing the string from right to left seems to be better as you dont need to course correct yourself.
@learntech39825 жыл бұрын
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
@carlestutusausmarrugat88895 жыл бұрын
the zero exists in roman number!! now it's "" ;)
@venkatp9684 жыл бұрын
Zero is place holder number, not actual value here
@ajeetworking4 жыл бұрын
thank you for the explaination
@RaghuDevBattula Жыл бұрын
Thank you Sir :)
@anonymous_998893 жыл бұрын
good
@janellogrono79893 жыл бұрын
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; } }
@amanroy84475 жыл бұрын
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!
@chethan934 жыл бұрын
When i = s.length() - 1 in the iteration, fetching i+1 character from s will throw exception..
@arijeetbhattacharya97874 жыл бұрын
@@chethan93 we can check that i is in between 0 and the last index too in the loop
@sharikkumar85814 жыл бұрын
it does not pass all the test cases in leet code. but thanks for showing the approach
@aravindrajesh44843 жыл бұрын
CCXLVII is 247
@learntech39825 жыл бұрын
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-cg3fv2 жыл бұрын
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; } }
@cjm1012 жыл бұрын
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 🙏🏾
@roushanraj85303 жыл бұрын
For this space complexity will be O(1) right, as you are storing only 7 values in the map for complete program....?
@satishthakur2156 Жыл бұрын
one thing while Testing XXL -getting 50 ideal it should be 30 right ?
@nrakshi30604 жыл бұрын
How to check for invalid scemarios like IIII or VV ?
@JohannGambolputty863 жыл бұрын
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 Жыл бұрын
why not start from i=1 so we do not have to check if i>0 in the if clause?
@akshaykumar-uv3up5 жыл бұрын
When you make video on spring boot with ouath2 integrated with api gateway.
@arian.mohajer2 жыл бұрын
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?
@NancyC82 жыл бұрын
I ran XIIV in leetcode it said this string is not a valid roman integer.
@sangharshadhital9610 Жыл бұрын
you have to add more logic on 5 ,99 and character repeated more than 3 times
@venkatagirivatlam96243 жыл бұрын
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-36153 жыл бұрын
My brain liked this more .... for(int i =0; i
@Seemakrid4 жыл бұрын
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
@kombophoto3 жыл бұрын
such number will not be provided, so you dont have to worry about such case. you got correct logic tho
@TheCasualGamer064 жыл бұрын
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
@amitgautam60193 жыл бұрын
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.
@narendramuppala44484 жыл бұрын
Only a programmer would laugh at the “charAt” reference 😄
@nishant52494 жыл бұрын
What's the problem with charAt🙄
@narendramuppala44484 жыл бұрын
@@nishant5249 He pronounced it as Carrot and then corrected is a CharAt and laughed about it. 5:00
@cvxcfv3 жыл бұрын
I think you're a brilliant Engineer but you need to rehearse more on your script...
@johnsondurant99054 жыл бұрын
In this line of code, if (i > 0 && s.charAt(i) > s.charAt(i - 1)), why do we need i > 0? I'm confused.
@nribackpacker4 жыл бұрын
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)
@benfowlie51474 жыл бұрын
The i is your index into the String. If i=0, what happens with the charAt 0-1? It would be an error.
@learntech39825 жыл бұрын
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
@mdsiddiq41455 жыл бұрын
Hi koushik please continue with spring security oauth2 with roles..
@Java.Brains5 жыл бұрын
Yes, an OAuth explainer coming this Friday/Saturday. Will continue the series after that.
@yadneshkhode30915 жыл бұрын
Thank you
@shivisaxena27982 жыл бұрын
You worked for 247 not 248
@bhavinmajethia70703 жыл бұрын
Isn't it 248 should be CCXLVIII ??
@mateuszperyt79983 жыл бұрын
yes?
@mirandahobbes81893 жыл бұрын
why is this not giving me correct result?:(((((
@mateuszperyt79983 жыл бұрын
it does, works fine for me on eclipse
@sharathchandrareddy89594 жыл бұрын
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"
@santoshjackman2 жыл бұрын
its "DC"
@aradhanasinghal1385 жыл бұрын
Core java please
@DT-oo5mp5 жыл бұрын
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.
@shrad66112 жыл бұрын
if you dont like his face but like the knowledge jump to 4:31 , thanks me later
@itsourav3 жыл бұрын
10 min ka video bane ke liya starting 4 min bakwas kar diya..... Everyone start watch after 4 min