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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the question is the other way around Roman numeral doin teacher but all this you know about Roman numerals now should be fairly simple to convert it the other way around again the characteristics of Roman numerals that you have kind of discovered in this question should help you with this process it is a slightly more complicated effort than the previous one where we had like positioning nothing you cannot do the mapping anymore unfortunately so because you have a whole lot of combinations right you cannot do like a one-to-one mapping but integer to the Roman you had like 10 symbols but here you have many many more so mapping is not an efficient strategy you to come up with something else hint you can still not individual symbols but the combination is something that you gonna have to work out at runtime so again the questions what's the range we're looking at what's the maximum number that we can get again it's gonna be above 1 for sure because Romans didn't have 0 what's the maximum let's say it's three thousand or something 3499 whatever you get whatever is the maximum Roman numerals you can get that up till 10 has the symbol right that's your maximum number give it a try let's look at the pseudocode at least let's see the way I'm gonna show you the way I would approach it and then we can discuss so let's take a simple use case which I told you that Roman numerals are both a dick additive and subtractive so let's say for something like three basically have three eyes and all you need to do is the add of the symbols up the value of all the symbols up and let's say 300 CC see what you need to do is add all the symbols up so you're looking at the pure additive case notice you don't have those fancy one from ten or like one from 500 if I endure whatever that the negative case right if it's just additive all you're doing is setting up a mapping of symbols to numbers right and when you get a symbol you find out what the number is that maps do that they basically add everything up right in this case it just doesn't matter which direction you go you basically look at each digit 1 hi is one okay I get one there's one more I ok get one more one had it becomes two and then you go to the next is there an X okay X is what it's ten okay at ten found one more hex okay add one more to answer it's very simple that we write is just the additive case it's very simple the tricky part you need to handle is the subtractive case subtractive cases where you have a smaller value in front of a bigger value in which case what you're doing is you're gonna have to subtract the smaller value from the bigger value so when you're taking that number again let's go back to that approach one by one licky taking each digit looking at the value and then adding it up to the result of Emperor you just don't add the result lightly you also check if the value is smaller than the previous value but you see the previous digital is hundred and this digit is ten well then it clearly 10 doesn't have to be added them has to be subtracted there based on that you do the subtraction in strip the addition alright so if you do this that's essentially all you need to do right you'll handle the additive case which is just blindly adding the numbers as long as then the digit to the right of it is the same or less than the digit to the left to the digit that you're considering however at the digit to the right of the digital considering is greater then you have to subtract you have to do something different right so that's the approach that I would go with and then we can we can discuss if there are other ways to handle this right so let's say I have what was the number that we talked about 248 let's take that 248 so this as we found this CC XL v hi hi alright you can go with either direction you can go from right to left or you can go from left to right I'm gonna go from left to right because it's gonna give me an I equal to 0 to string length loop just easier so I'm gonna go from I equals 0 to straight length I'm gonna get Kat it off Kat at not the cat it cat at off a high and get 1 by digit one by one right so first I'm gonna get C and I'm assuming here that there is a map somewhere the Roman numeral to the numbers let's say I have I is mapped to 1 and then V is mapped to 5 X's marked 10 C is mapped to 100 and then L is mapped to 50 right so I get see what am I gonna do I'm gonna see what the value is I'm just gonna add hundred so there's the result just mad hundred let me go to the next one on another see I'm just gonna add another 100 go to X what is X X is 10 mad at em here and then let's go to L wait a minute L is greater than X this is supposed to be going downwards but L is greater than X L is 50 while X is 10 so what I'm gonna do is I'm gonna add L now which is 50 but I'm going to undo this X whatever I added before and I'm gonna do X as - step because since it's the other way around I need to do a subtraction when keep going V is 5 when I add 5 and then Y is 1 and then another I is 1 and then I be they add these up so the idea is just go blindly keep adding the numbers but always check what's to the what was there before right so in this case I was looking at you know I was looking at X is it okay fine I'm just going to add it I'm not even thinking but then the minute I find something which is greater than what I'd encountered before I'm gonna say oh no hang on I should not be adding the previous number that should actually be subtraction so I first take out the thing that I added and then I take this actual value from which it needs to be subtracted and then I actually do the subtraction it's kind of like course-correcting if I encounter a subtractive use case right it's fairly simple you can do you can do various different approaches you can probably do from the right where you don't have to do this course correction in a way but either approach is fine I'm going to show you the approach that I have in the code then you know what you think Roman doing teacher takes in a string is an argument and it returns an integer what does it do first creates this map do you remember the map I had on the right hand side which basically maps a Roman character to a number I'm just gonna put all those things that I'm gonna say is 1 v is 5 X is 10 all the way up to M which is what we are looking for because once I encounter I'm gonna be cat I'm gonna be scanning the Roman numeral string character by character and for each character I need to pull up the value so that's what this is for so I'm gonna start with the result equals 0 and then I'm gonna loop through the lengths looking at each character and then the editor views case basically result plus equals map get off that character right string dot character at if we're just doing the additive use case without the subtractive use case this will work however we need to handle the subtractive use case which is basically if there is a character a lower character in front of a higher character so what I need to do is not just do this this should be done only if that particular check hasn't been met so I'm gonna put an if block there and say vice greater than zero and map darkeneth the character at I is greater than NAB dot get at the character of I minus one okay so if what you get over here is greater than what you got before in that case what you added before was a mistake so you undo that and then do this - the previous one which is what I'm gonna do here I'm basically gonna get that value which is the greater value and then I'm going to remove the previous smaller value twice first when I'm gonna remove because oops added that back mistake if is not supposed to be an additive one it was supposed to subtract from this guy so I'm gonna undo that by subtracting once and then I'm gonna take this guy minus the previous one so I'm going to subtract it one more time which is what I'm gonna do to star the value of the previous one so for example if 90 is X and C extends for 10 and C stands for hundred so we're just going blindly I would say X is 10 I'm going to add it and then C is 100 okay now I realize I should have added that 10 so I'm gonna subtract that and I'm gonna do 100 minus 10 so I'm essentially subtracting 10 twice which is what the two star the previous value is alright so this was the solution for tackling the Roman to integer hopefully this makes sense
Info
Channel: Java Brains
Views: 31,408
Rating: 4.8931084 out of 5
Keywords: roman to integer leetcode, java brains, roman to integer
Id: hEhf_oz3wsM
Channel Id: undefined
Length: 10min 38sec (638 seconds)
Published: Tue Nov 19 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.