Basic Calculator II | Leet code 227 | Theory explained + Python code

Sdílet
Vložit
  • čas přidán 27. 07. 2024
  • This video is a solution to Leet code 227, Basic Calculator II. I explain the question, go over how the logic / theory behind solving the question and finally solve it using Python code.
    Comment below if you have a better solution to this problem!
    Let me know if you have any feedback and don't forget to subscribe for more videos!
    Time stamps:
    0:00 Question Explained
    2:46 Solution Explained
    10:17 Python Code
    Code:
    leetcode.com/problems/basic-c...)
    More leetcode questions solved:
    • Add and Search Word - ...

Komentáře • 28

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

    This is the best explanation on the problem I've seen. After your explanation I was able to code it out on my own. Thank you so much.

  • @jamesperalta2870
    @jamesperalta2870 Před 3 lety +13

    For 18:30, the reason it wasn't coming out correct was because the question says "The integer division should truncate toward zero." When you perform integer division, you get the floor which would result in rounding -0.333 to -1 when it should round to 0 based on the description. int() however rounds towards 0 which is why it works.

    • @saianishmalla2646
      @saianishmalla2646  Před 3 lety

      Thanks for explaining!

    • @thechunwu
      @thechunwu Před 2 lety

      In a situation like: "14-3/2" I find the int() rounds to -2 rather than -1 because in this case its rounding down. which is a greater negative number?

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

    Great video! A further optimization would be to eliminate the stack altogether and keep a running sum. Instead of appending to the stack, just add. Instead of popping, just keep track of the most recent value and subtract it off. Then you can save yourself the extra loop to go through and sum all the values. Specifically, it will bring storage complexity from O(n) to O(1) and reduce time by a factor of 2 (though it will still be O(n)).

  • @n.h.son1902
    @n.h.son1902 Před 2 měsíci

    Great visualization! It helps build my intuition a lot for this challenge. There're a couple of ways to handle the edge case about the operator == "/", one way we can think of is to take advantage of the built-in function math.floor() and math.ceil(). Cheers!

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

    this is one of the best coding teaching videos so far, nice voice, cadence, explanation, and visuals

  • @kevinzheng575
    @kevinzheng575 Před 3 lety +1

    Nice information. Thank you!

  • @grovestreet9165
    @grovestreet9165 Před 2 lety +2

    if you're indian then your accent is realy too good

  • @user-fx7ur9uy3m
    @user-fx7ur9uy3m Před 6 měsíci

    this really cleared things up!

  • @sakshiramsinghani5284
    @sakshiramsinghani5284 Před 3 lety +1

    Thanks a lot! It's really helpful.

  • @vidyutphagetra3804
    @vidyutphagetra3804 Před 3 lety +1

    good explanation Anish

  • @linli7049
    @linli7049 Před 3 lety

    Very good explanation!

  • @ishayadav001
    @ishayadav001 Před 3 lety +1

    Love your videos!!

  • @sugamff8825
    @sugamff8825 Před 3 lety +3

    Big fan bro ❤️

  • @LARamones23
    @LARamones23 Před 3 lety +1

    Thanks for the nice explanation! Your videos are very helpful.
    Integer division for negative number is the issue for stack[-1] //= curr_num. For example -3//4 returns -1 in python but 3//4 returns 0.
    Following approach seems also to be working.
    if stack[-1] >=0 :
    stack[-1] //= curr_num
    else:
    stack[-1] = -(-stack[-1]//curr_num)

    • @saianishmalla2646
      @saianishmalla2646  Před 3 lety

      Glad the videos help! Thanks for explaining why int division didn't work it makes sense.

  • @Chancemm
    @Chancemm Před 3 lety

    Thank you!!

  • @shrimpo6416
    @shrimpo6416 Před 2 lety

    Bc int div round towards zero. So it gets weird when rounding neg num

  • @user-kg9lm4mp8j
    @user-kg9lm4mp8j Před rokem

    //是整除,int()是取整,题目要求向0取整,整除遇到负数会出错。

  • @aminesfahani3563
    @aminesfahani3563 Před 3 lety +1

    thanks
    i want to share this code
    class Solution:
    def myPow(self, x: float, n: int) -> float:
    if n==0:
    return 1
    elif n

  • @diegogarcia-pl1bt
    @diegogarcia-pl1bt Před 2 lety +3

    you just stole the answer from the discussion you clearly do not understand the code

    • @PremPal-uy4nm
      @PremPal-uy4nm Před rokem

      It seems he didn't explain clearly how char & operator are working together to add values in stack to make correct calculation as we move through the string. In short char is leading in for loop and operator is lagging.

    • @n.h.son1902
      @n.h.son1902 Před 2 měsíci

      @@PremPal-uy4nm if I remember it right, he did. His explanation in the visualization meant that the variable “operator” in the code is the previous operator in the illustration and the variable “char” means the current operator we are at.