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

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 ต.ค. 2024

ความคิดเห็น • 100

  • @centurion8158
    @centurion8158 4 ปีที่แล้ว +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 ปีที่แล้ว +9

    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 ปีที่แล้ว +8

    You start recording more question like this and solve them will help us get different approach .
    thanks

  • @SuperYouthful
    @SuperYouthful 4 ปีที่แล้ว +2

    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???

  • @DougieMuringani
    @DougieMuringani 4 ปีที่แล้ว +5

    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.

  • @SomeoneAlive90
    @SomeoneAlive90 2 ปีที่แล้ว +2

    Love this channel!

  • @vishal24000
    @vishal24000 4 ปีที่แล้ว +3

    Loving these problem solving videos. Keep 'em coming :D

  • @caseycampbell8047
    @caseycampbell8047 2 ปีที่แล้ว +2

    Honestly this is the first time I've understood this problem after watching this video. Great explanation!

  • @ritikbhardwaj4061
    @ritikbhardwaj4061 4 ปีที่แล้ว +1

    Really thanks man, for this so crystal clear explanation.... my first video forced ....me to hit subscribe...thanks again...

  • @shubhamsaurabh4419
    @shubhamsaurabh4419 5 ปีที่แล้ว +2

    Sir please make a video on how to get all permutations of a string.
    Please solve it without recursion.

  • @rajeshpaithari7320
    @rajeshpaithari7320 8 หลายเดือนก่อน

    Great Explanation!! Thanks

  • @GagandeepSingh-lz5bg
    @GagandeepSingh-lz5bg 2 ปีที่แล้ว +1

    Very good explanation, to the point. Thank you.

  • @jnayehsirine6222
    @jnayehsirine6222 ปีที่แล้ว

    LOVE THIS CHANNEL

  • @namratam1522
    @namratam1522 4 ปีที่แล้ว +1

    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.

  • @Ariesgod1998
    @Ariesgod1998 ปีที่แล้ว

    Bro I need the 150 interview questions on leetcode from you , Man great explaination

  • @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?

  • @evgenii-govorushkin
    @evgenii-govorushkin ปีที่แล้ว

    Great explanation. Thank you!👍

  • @continnum_radhe-radhe
    @continnum_radhe-radhe ปีที่แล้ว +1

    🔥🔥🔥

  • @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. :)

  • @MaggicBones
    @MaggicBones 3 ปีที่แล้ว

    Awesome !!!

  • @SurendraBabuK
    @SurendraBabuK 5 ปีที่แล้ว +1

    Please continue for some more questions. It's really appreciate your efforts

  • @maarif1869
    @maarif1869 5 ปีที่แล้ว +1

    Please do a video about remember me option with session expire problem. You are an awesome teacher.

  • @ShaliniNegi24
    @ShaliniNegi24 4 ปีที่แล้ว +1

    Very nicely, Explained!
    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;
    }

  • @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;
    }

  • @kibizoid
    @kibizoid 5 ปีที่แล้ว +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 ปีที่แล้ว +2

      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.

  • @saddamahmad2310
    @saddamahmad2310 5 ปีที่แล้ว +2

    thank you very much sir for this video

  • @vidhi_bhatt
    @vidhi_bhatt 2 ปีที่แล้ว

    Well explained!

  • @adeshgaikwad2192
    @adeshgaikwad2192 4 ปีที่แล้ว +3

    Doesn't he sounds like Rohit from Koi Mil Gaya...Aaila Jaadu

  • @neludonellyakejellu1914
    @neludonellyakejellu1914 4 ปีที่แล้ว

    Very succinct explanation. Great Job

  • @namratam1522
    @namratam1522 4 ปีที่แล้ว +1

    This made me take more interest in programming

  • @sagnikghoshcr7
    @sagnikghoshcr7 4 ปีที่แล้ว

    Approach is very good

  • @learntech3982
    @learntech3982 4 ปีที่แล้ว

    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

  • @SR-we1vl
    @SR-we1vl 4 ปีที่แล้ว

    Superb!

  • @kingop2679
    @kingop2679 14 วันที่ผ่านมา

    Thanks sir

  • @keshavdeosharma7222
    @keshavdeosharma7222 9 หลายเดือนก่อน

    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!

  • @amanroy8447
    @amanroy8447 4 ปีที่แล้ว

    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 ปีที่แล้ว +1

      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

  • @YazhShah
    @YazhShah 3 ปีที่แล้ว

    Wow I like the way you explained it.

  • @ajeetworking
    @ajeetworking 4 ปีที่แล้ว

    thank you for the explaination

  • @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 🙏🏾

  • @anonymous_99889
    @anonymous_99889 3 ปีที่แล้ว

    good

  • @RaghuDevBattula
    @RaghuDevBattula ปีที่แล้ว

    Thank you Sir :)

  • @sharikkumar8581
    @sharikkumar8581 4 ปีที่แล้ว +2

    it does not pass all the test cases in leet code. but thanks for showing the approach

  • @satishthakur2156
    @satishthakur2156 ปีที่แล้ว

    one thing while Testing XXL -getting 50 ideal it should be 30 right ?

  • @dev-skills
    @dev-skills 3 ปีที่แล้ว

    Traversing the string from right to left seems to be better as you dont need to course correct yourself.

  • @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;
    }
    }

  • @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....?

  • @learntech3982
    @learntech3982 4 ปีที่แล้ว

    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

  • @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.

  • @carlestutusausmarrugat8889
    @carlestutusausmarrugat8889 5 ปีที่แล้ว +3

    the zero exists in roman number!! now it's "" ;)

    • @venkatp968
      @venkatp968 4 ปีที่แล้ว

      Zero is place holder number, not actual value here

  • @sysybaba420
    @sysybaba420 ปีที่แล้ว

    why not start from i=1 so we do not have to check if i>0 in the if clause?

  • @nrakshi3060
    @nrakshi3060 4 ปีที่แล้ว +2

    How to check for invalid scemarios like IIII or VV ?

    • @JohannGambolputty86
      @JohannGambolputty86 3 ปีที่แล้ว +1

      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.

  • @akshaykumar-uv3up
    @akshaykumar-uv3up 5 ปีที่แล้ว

    When you make video on spring boot with ouath2 integrated with api gateway.

  • @learntech3982
    @learntech3982 4 ปีที่แล้ว

    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

  • @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

  • @aravindrajesh4484
    @aravindrajesh4484 3 ปีที่แล้ว +1

    CCXLVII is 247

  • @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)..

  • @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

  • @janellogrono7989
    @janellogrono7989 3 ปีที่แล้ว +1

    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;
    }

    }

  • @sangharshadhital9610
    @sangharshadhital9610 ปีที่แล้ว

    you have to add more logic on 5 ,99 and character repeated more than 3 times

  • @cvxcfv
    @cvxcfv 3 ปีที่แล้ว

    I think you're a brilliant Engineer but you need to rehearse more on your script...

  • @TheCasualGamer06
    @TheCasualGamer06 3 ปีที่แล้ว

    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.

  • @sinsere-3615
    @sinsere-3615 3 ปีที่แล้ว

    My brain liked this more ....
    for(int i =0; i

  • @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.

  • @mirandahobbes8189
    @mirandahobbes8189 3 ปีที่แล้ว

    why is this not giving me correct result?:(((((

    • @mateuszperyt7998
      @mateuszperyt7998 2 ปีที่แล้ว

      it does, works fine for me on eclipse

  • @mdsiddiq4145
    @mdsiddiq4145 5 ปีที่แล้ว

    Hi koushik please continue with spring security oauth2 with roles..

    • @Java.Brains
      @Java.Brains  5 ปีที่แล้ว +1

      Yes, an OAuth explainer coming this Friday/Saturday. Will continue the series after that.

  • @bhavinmajethia7070
    @bhavinmajethia7070 3 ปีที่แล้ว

    Isn't it 248 should be CCXLVIII ??

  • @shivisaxena2798
    @shivisaxena2798 2 ปีที่แล้ว

    You worked for 247 not 248

  • @yadneshkhode3091
    @yadneshkhode3091 5 ปีที่แล้ว

    Thank you

  • @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"

  • @aradhanasinghal138
    @aradhanasinghal138 5 ปีที่แล้ว

    Core java please

  • @DT-oo5mp
    @DT-oo5mp 5 ปีที่แล้ว +1

    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