Basic Calculator II | Leet code 227 | Theory explained + Python code
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 - ...
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.
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.
Thanks for explaining!
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?
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)).
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!
this is one of the best coding teaching videos so far, nice voice, cadence, explanation, and visuals
Nice information. Thank you!
if you're indian then your accent is realy too good
this really cleared things up!
Thanks a lot! It's really helpful.
good explanation Anish
Very good explanation!
Love your videos!!
Glad you like them!
Big fan bro ❤️
ayyeee thank you!!!
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)
Glad the videos help! Thanks for explaining why int division didn't work it makes sense.
Thank you!!
You're welcome !!
Bc int div round towards zero. So it gets weird when rounding neg num
//是整除,int()是取整,题目要求向0取整,整除遇到负数会出错。
thanks
i want to share this code
class Solution:
def myPow(self, x: float, n: int) -> float:
if n==0:
return 1
elif n
you just stole the answer from the discussion you clearly do not understand the code
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.
@@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.