Convert Integer to Roman numeral - LeetCode Interview Coding Challenge [Java Brains]
ฝัง
- เผยแพร่เมื่อ 22 ส.ค. 2024
- Interview Question: Convert Integer to Roman numeral
Leetcode link: leetcode.com/p...
Solution Gist: gist.github.co...
Difficulty level: Easy
Language: Java
Welcome to the Java Brains Java Interview challenge series of videos where we tackle various coding challenges ranging from the beginner level to advanced level - especially in the context of coding interviews.
Any coding problems you would like me to explore? Please let me know in the comments what you think!
#java #interview #javabrains
As you have said in the end, sometimes we complicate fairly simple tasks. I was thinking about using switch case , hashmap and whatnot before I saw your approach to the question. Amazing explanation.
Zvgdbdgsbshdbhysdhvxgdgebebsyydhnxnxnhybxsbsnh hj d vc TB hj
Bvgz gsbhggbevgrhvfgggstztyyggf
Gv
Nnjbdbsbshs7dududuuddduydyydydd7yhyxyyddhhdd6hhhshdyhvhdhnjfbhhghhfgedbzgfbdehhdhejbznzhshsjsshbjnshbzb um yt
same with me
After so many days, I recollected my childhood days, where I use to play with Roman numbers, but completely forget when days have grown. Now it's because of u, I recollected them and felt enjoying by looking at your video. Thank you so much sir.
Yeeee! I love you Man! Thanks a ton for every contribution you made in my career!
Wow. I did not know you started sharing leetcode questions. This is awesome. Is there a playlist?
Thanks! Here you go: th-cam.com/play/PLqq-6Pq4lTTZgXnsBNQwCWdKR6O6Cgk1Z.html
Not a lot yet, but there will be more coming up. Make sure you are subscribed with notifications if you don't want to miss out! :)
This is super awesome Koushik. Thanks for sharing. Your videos have helped me for years. You make extremely complicated things very simple. I will keep looking for this space.
Thanks for sharing this gem. If I had not gone through this video I would have chosen a much complex approach to solve this. This solution teaches that for some 'COMPLEX-SOUNDING' problems. A simple and feasible solution exists. All we need is to stick to the basics...
Thanks again for this wonderful tutorial Kaushik..God bless you.
This is such a great video, I remember the first time I encountered it in school. I went a different more convoluted route.
One of the best optimised solution for int to roman conversion. your just awesome man
wow! what a nice thinking process. many TH-camrs share logic, but to my surprise, you speak about the thought process. Thank you so much sir. can you please make leetcode playlist for all the medium level problems
Saiman says teaching java, amazing...
thumbs up :), and sorry for prev comment on last video
When Roman Numerals were invented, there was no concept of Zero, and nobody had yet thought about negative numbers as a concept.
And here I was so proud of my code:
"int unit = value % 10;
value /= 10;
int dozen = value % 10;
value /= 10;
int cent = value %10;
value /= 10;
int mil = value %10;
value /= 10;
return thousands[mil]+hundreds[cent]+tens[dozen]+units[unit];"
Lot of repeated stuff honest work, then I saw the video results..... sooo much to learn =/
Amazing! my Primary-2 kid actually have this Math topic of ones, tens, and hundreds.. This is an amazing solution. Thanks so much sensei Kaushik. :)
Please do more of these type of problems. Your explanation is very good and clear. Expecting more like this
Greetings from Russia. Thanks!
super one Kaushik. the approach you take to these problems is very intuitive . Thanks for this video.
Interesting. I tought we would need to do the parse at one point. This is an excellent solution.
@Java Brains - Super Awesome..!! I like the "what kind of questions you can ask from Interviewer" part of your video, this usually youtuber don't tell it. Please go ahead with Leetcode series.
Your videos keep getting better and better. Kudos!
wow...my solution was....quite....extensive.....
thank you for this lol
I just answered this question on leetcode and it took me 95 lines of code to get a solution
This will work up to 3000. You can make it work easy up to 10000 or to max_value from short (just don't pass int as argument as this can go from -2147483647 to +2147483647to . What Romans did was to add a line over a number meaning they were multiplying that number by 1000. So, if you needed to write 9000 you had IX with a line over it. So for a max short (32768) you would have the tens of thousands XXX with a line over it and then use your function to append to it.
Thank you so much for making the question so easy to understand
Thanks for sharing these.
Very helpful, It would be appreciable if you make video on cache mechanism in spring and software perform. Thanks in advance
Nyc one sir
Thanks for the great explaination Kaushik. Kudos to you
Why do I have a fear while watching your video like you are my interviewer and I am the student ? LOL :)
Videos are always awesome btw
Your voice is like sundar pichai
Amazing sir!! Truly most clear explanation! : )
you explain so perfectly
Please try uploading more and more algorithmic questions where getting logic is difficult. Example: sliding window alogos etc.
Nice one
Thanks you so much sir!!! This solution is amazing!!! Wonderful!!!
Maja aa gaya. In the beginning i thought that this is a very tough question but the way you solve it and the solution is no doubt amazing!!! Thanks once again sir...
Thanks a ton for this video, I love the way you explain the concepts, would request you to add more such unique Java Interview question videos to this series. Thanks.
Hi, romans didn't have the concept of negative numbers, the only use natural numbers without the 0 number
Hi Koushik, First of all, thank you for all your lectures. Your video quality is excellent. Would you like to put list of resources you use for the video making? It will help a lot for starters like me who interested to start a youtube channel. Tools like what whiteboard app you are using, are you using apple pencil etc. Also if you have any affiliate links, Please provide them. We can use those links to purchase that help your channel too. Thanks Mate
Thanks :)
Int Roman = input();
Int divide = 0;
Int thousand;
Int fiveHundred;
Int oneHundred;
Int fifty;
Int ten;
Int five;
Int one;
Thousand = Roman / 1000;
Divide = Roman - 1000* Thousand;
FiveHundred = Divide /500;
Divide = Divide - 500 * FiveHundred;
OneHundred = Divide /100;
Divide = Divide - 100* Onehundred;
Continue for 50 and 10 and 5 and 1...
If (onethousand > 0){
For (i=onethousand, i--, i>0)
System.out.print("M");
Continue for the REST OF the Num.
Greatly explained!!
Amazing logic, Sir!
✌
thank you very much sir for this video
950 should be CML or LM?
CML
Hi sir i'm an intern SE and I have been following your content for sometimes. It took me more than 6hrs to complete this question. How can I improve my skills on these type of questions.I'll attach my code in another comment . Thank you for you content. Guidance from the community would be also nice :).
public class Calculator {
public static void main(String[] args) {
String s = integerToRoman(1994);
System.out.println(s);
}
private static String integerToRoman(int input) {
int[] intArr = {1,4, 5, 9,10,40, 50,90, 100,400, 500, 900,1000};
int module = 0;
int value = input;
StringBuilder stringBuilder = new StringBuilder("");
for (int index = intArr.length - 1; index >= 0; index--) {
module = value / intArr[index];
stringBuilder.append(stringGenerator(index,module));
value = value- module* intArr[index];
}
return stringBuilder.toString();
}
private static String stringGenerator(int index, int module) {
String[] romanArr = {"I","IV", "V","IX", "X", "XL","L","XC", "C","CD" ,"D","CM", "M"};
String s = "";
for (int i = 0; i < module; i++) {
s = s + romanArr[index];
}
return s;
}
}
Thanks man!
👌
Thanks, bro
I love your chanel, Thank you very much for the content.
About this question, the answer you gave is not correct, for example 499 in roman is "ID" but your code returns "CDXCIX".
NoUuuuuuuu, your´re wrong, He´s right🤥
The first array value of units has a “ not a ".
Hello sir, waiting for your oauth2 tutorials..
At the start of the problem it looked like a mount everest but after the explanation it looked look nothing
6:10 i did 🤣
why didn't you end
can u also mention the time and space complexity please?
t(n) = o(number of digits in n)
How do you get the index as the actual digit? Index would be 0,1,2,3 and so on.
Because for 0th index he is storing empty value. hence for each index correct value is picked up. if you don't store empty index at first, then you would have to fetch by array[index -1]
lol this question😂😂😂
wow we have somewhat the same solution wtf..
Wow, but this solution is slow, just beat 20% of other solutions in Java. Maybe too many calculations in return result that slow.
That is O(1) time and O(1) space, you are cheating, men:)
U kinda talk a lot
Why don't you use an array of array where the first level is indexed by p = [0, 1, 2, 3] that you can use as [10**0, 10**1, 10**2, 10**3] = [1, 10, 100, 1000] ?
So if you loop on p--, you can get every thing with romans[ p ][ num / 10**p ]