BASIC CALCULATOR II | LEETCODE 227 | PYTHON SOLUTION

preview_player
Показать описание
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!
Рекомендации по теме
Комментарии
Автор

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!

yuqian
Автор

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

johnangelo
Автор

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

elikiperwasser
Автор

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.

patb
Автор

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

Ankit-hsnb
Автор

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

TheAdityabhandari
Автор

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

vishnuteja
Автор

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.

hooriehmarefat
Автор

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!

zuccca
Автор

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

kalyanvejalla
Автор

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.

ye
Автор

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

wayne
Автор

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

symbol
Автор

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

yingxu
Автор

Thank you soo much! You are appreciated!

dnm
Автор

great explanation of the intuition and solution.

bellicosex
Автор

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

kapilrules
Автор

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

naaz
Автор

Great video, keep those coming, nice job

darleisoares
Автор

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.

kalp