BASIC CALCULATOR II | LEETCODE 227 | PYTHON SOLUTION

Поділитися
Вставка
  • Опубліковано 28 жов 2024

КОМЕНТАРІ • 43

  • @yuqian822
    @yuqian822 9 місяців тому +9

    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  9 місяців тому +1

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

  • @elikiperwasser
    @elikiperwasser 3 місяці тому +3

    What about multiple multiplications like 2*3*4? The code you wrote will try to undo a summation but there isn't one..

  • @Ankit-hs9nb
    @Ankit-hs9nb 2 роки тому +5

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

  • @johnangelo2000
    @johnangelo2000 10 місяців тому +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 5 місяців тому +1

    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.

  • @TheAdityabhandari
    @TheAdityabhandari 2 роки тому +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  2 роки тому +2

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

    • @n6i9k4a
      @n6i9k4a Рік тому +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  Рік тому +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 7 місяців тому

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

  • @kalyanvejalla
    @kalyanvejalla Місяць тому +1

    This solution fails for the test case "14-3/2". Yours returns 12 when the expected is 13. I had to do int(float(prev) / current) for division instead

  • @zuccca
    @zuccca 9 місяців тому +2

    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  9 місяців тому +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 2 роки тому +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.

  • @vishnuteja9758
    @vishnuteja9758 Місяць тому

    For division, if you want to truncate to 0, in python3 we would have to use int(float(prev) / curr)

  • @ye7841
    @ye7841 2 роки тому +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  2 роки тому +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 2 роки тому +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!

  • @yingxu1694
    @yingxu1694 29 днів тому

    always do res -= prev when encountering * and /, what if the previous operation is -

  • @wayne4591
    @wayne4591 Рік тому

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

  • @kapilrules
    @kapilrules Місяць тому

    how did you assumed that curr_operation will always start with +? what if start with other operator?

    • @Andrew-dd2vf
      @Andrew-dd2vf Місяць тому +1

      It's because the first number we encounter is always "added" to the total sum "res", initialized to 0. Let's consider an edge case of one number, say s = [2]. we know that the result should be 2, and you can think of it as 0 + 2 (where 0 is the previous number and + is curr_operation). If you used " * ", the result for this case would be 0 (since 0 * 2), which we know is clearly false

  • @symbol767
    @symbol767 2 роки тому +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  2 роки тому +5

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

    • @daxtermaster6793
      @daxtermaster6793 9 місяців тому

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

  • @bellicose100x
    @bellicose100x Рік тому

    great explanation of the intuition and solution.

  • @darleisoares23
    @darleisoares23 2 роки тому +1

    Great video, keep those coming, nice job

    • @crackfaang
      @crackfaang  2 роки тому

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

  • @dnm9931
    @dnm9931 3 місяці тому

    Thank you soo much! You are appreciated!

  • @devinenivenkatesh6101
    @devinenivenkatesh6101 Рік тому +1

    Thanks you sir Nice solutions

    • @crackfaang
      @crackfaang  Рік тому

      No problem glad you liked the video

  • @naaz2659
    @naaz2659 4 місяці тому

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

    • @Andrew-dd2vf
      @Andrew-dd2vf Місяць тому

      because multiplication / division takes precedence over addition / subtraction. E.g. 2 * 4 + 3, after you encounter the number "4", your prev will be updated to 2* 4 = 8. And so when a new operator (in this case, +) is encountered, you can simply add 3 with (2*4) instead of, say, 4 individually, which would clearly be wrong

  • @sarayarmohammadi3376
    @sarayarmohammadi3376 23 дні тому

    Thank you

  • @roywastaken
    @roywastaken Рік тому

    GOAT solution... i used a stack lol

  • @tobychow4761
    @tobychow4761 9 місяців тому

    love this sol

  • @AmolGautam
    @AmolGautam 11 місяців тому

    Thank you so much.

  • @subee128
    @subee128 9 місяців тому

    Thanks

  • @kalp2586
    @kalp2586 4 місяці тому

    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  4 місяці тому

      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

  • @aditijain2448
    @aditijain2448 Рік тому

    good code