BASIC CALCULATOR II | LEETCODE 227 | PYTHON SOLUTION

Sdílet
Vložit
  • čas přidán 14. 03. 2022
  • In this video we are solving one of the problems in the Basic Calculator saga: Basic Calculator II. It's arguably the easiest of the Basic Calculator problems and currently a popular Facebook interview question.
    It's actually not a hard problem and can be solved in a very efficient manner if you can keep track of the state of the calculation and handle the two edge cases correctly: multiplications and divisions. Stay tuned to find out how!
  • Věda a technologie

Komentáře • 35

  • @yuqian822
    @yuqian822 Před 6 měsíci +5

    Awesome solution! This is much easier to understand and remember than the stack solution. I went with the stack solution before but I always missed some details or forgot certain lines of code every time I reviewed this problem... I hope this solution is acceptable by the interviewer. (I wouldn't be that picky if I am an interviewer but who knows). Thanks again!

    • @crackfaang
      @crackfaang  Před 6 měsíci +1

      No problem! Glad you found the video helpful and the solution was intuitive for you

  • @johnangelo2000
    @johnangelo2000 Před 6 měsíci +1

    This made me think how can one ask brain to observe operation without complicating them. This is awesome... I couldn't do it first with this clarity.. Thanks a bunch for clear thought..

  • @pat777b
    @pat777b Před měsícem

    For the int(pre / cur) vs pre // curr, I think i would ask the interviewer a clarifying question about how they want integer division to behave first. I'd just go in the direction they suggest me to. If they want to truncate to zero as the leetcode problem does, then i would go with int(pre / cur) as you suggest. I'd adjust appropriately depending on what the interviewer wants.
    I got to ask these clarifying questions cause sometimes the interviewers do twists.
    Nice solution btw.

  • @dnm9931
    @dnm9931 Před dnem

    Thank you soo much! You are appreciated!

  • @wayne4591
    @wayne4591 Před rokem

    Really good explanation! Thanks! The idea of undo the previous operation the really good as well as the set prev = prev * cur one.

  • @bellicose100x
    @bellicose100x Před rokem

    great explanation of the intuition and solution.

  • @TheAdityabhandari
    @TheAdityabhandari Před rokem +5

    If you're not using python3, the solution only gets accepted if you convert the res in the division to int(float(prev)/cur)

    • @crackfaang
      @crackfaang  Před rokem +1

      I mean this is a solution in Python 3 so it’s not guaranteed to work line by line in other languages

    • @n6i9k4a
      @n6i9k4a Před 11 měsíci +1

      ​@@crackfaang in LC, there's still a distinction between Python and Python3 in the language selection, so if you try to submit this solution in "Python" vs "Python3", it won't pass all test cases without this modification. I didn't catch that at first and couldn't figure out why this solution wouldn't pass all the test cases. Thanks @TheAdityabhandari

    • @crackfaang
      @crackfaang  Před 11 měsíci +1

      ​@@n6i9k4a You should not be using anything except Python3. The other version of Python is for some reason still allowed despite Python2 being deprecated on January 1, 2020.
      All solutions are always for Python3

    • @akshaychavan5511
      @akshaychavan5511 Před 3 měsíci

      You should just take the floor value of the division without complicating things.

  • @zuccca
    @zuccca Před 5 měsíci +1

    This channel is nothing short of pure gold. I am preparing for FAANG interviews currently. Do you offer any kind of 1-on-1 coaching? Please let me know!

    • @crackfaang
      @crackfaang  Před 5 měsíci +1

      Hey thanks for the very kind words. I do actually! If you join as a channel member at the $50/month tier you get 1 mock interview per month and at the $100 tier you get 2 per month + the perks of the lower tiers like resume review, private Q&A group with me, etc. You can check it out on my channel page by clicking the "Join" button to see the options.
      You can cancel it after your interviews when you no longer need it of course :)

  • @hooriehmarefat5972
    @hooriehmarefat5972 Před rokem +4

    Nice solution! Thanks.
    As a suggestion, if you can make the code page clearer, that would be awesome because the code page is very blurry and not easy to see.

  • @Ankit-hs9nb
    @Ankit-hs9nb Před 2 lety +3

    5:27 I think a small correction, the previous number is 2 not 3

  • @AmolGautam
    @AmolGautam Před 7 měsíci

    Thank you so much.

  • @darleisoares23
    @darleisoares23 Před 2 lety +1

    Great video, keep those coming, nice job

    • @crackfaang
      @crackfaang  Před 2 lety

      Thanks for the support! Make sure to subscribe so you don’t miss future videos

  • @tobychow4761
    @tobychow4761 Před 5 měsíci

    love this sol

  • @naaz2659
    @naaz2659 Před 27 dny

    Why are we setting Prev = cur * prev and not prev = cur?
    It will be very helpful if u answer this cause am stuck here

  • @subee128
    @subee128 Před 5 měsíci

    Thanks

  • @devinenivenkatesh6101
    @devinenivenkatesh6101 Před rokem +1

    Thanks you sir Nice solutions

    • @crackfaang
      @crackfaang  Před rokem

      No problem glad you liked the video

  • @roywastaken
    @roywastaken Před 10 měsíci

    GOAT solution... i used a stack lol

  • @ye7841
    @ye7841 Před 2 lety +1

    Great solution! Much easier to understand for a beginner ;) just wondering if there is a way to get rid of the i-=1 ? I think I would probably miss it if I got nervous in a real interview.

    • @crackfaang
      @crackfaang  Před 2 lety +1

      Yea you could just check that while s[i+1].isdigit() for the while loop instead. That would stop the iteration at the last digit you need to parse. I learned it the other way and that's what has stuck in my brain. Whatever works for you is best :)

    • @ye7841
      @ye7841 Před 2 lety +1

      @@crackfaang I need some help here: if using s[i+1].isdigit() then the last digit would not be added, right? e.g. 123+4*5, the 3 would not be added as the next char is not a digit...did I miss something here? Thank you so much!

  • @symbol767
    @symbol767 Před 2 lety +1

    Took me a while and I had to learn the stack solution first, I suggest everyone learn the stack solution because you can solve all 3 calculator problems with it, but after learning the stack solution I came back here and understood your solution much better. Then I managed to solve Calculator 1 and 3 on my own right after, the stack solution for Calculator 1 worked perfectly for Calculator 3 loll

    • @crackfaang
      @crackfaang  Před 2 lety +5

      Yea the stack solution is quite simple but the interview is basically always going to want the O(1) solution lol

    • @daxtermaster6793
      @daxtermaster6793 Před 5 měsíci

      Thanks, it's true, it becomes intuitive when we understand first the stack solution.

  • @aditijain2448
    @aditijain2448 Před rokem

    good code

  • @kalp2586
    @kalp2586 Před 20 dny

    Explanation at 4:47 is wrong. You want to undo the 3 + 5 operation, so it would be 8 - 5 , not 8 - 3. Best to avoid this approach. Stack based approach is much simpler.

    • @crackfaang
      @crackfaang  Před 20 dny

      Stack is easier but you will not pass with this question if you use extra space. You need to do it in O(1) space in a real interview unfortunately. Stack is considered too easy