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!
@crackfaang9 ай бұрын
No problem! Glad you found the video helpful and the solution was intuitive for you
@johnangelo20009 ай бұрын
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..
@pat777b4 ай бұрын
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.
@kalyanvejalla15 күн бұрын
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
@Ankit-hs9nb2 жыл бұрын
5:27 I think a small correction, the previous number is 2 not 3
@zuccca8 ай бұрын
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!
@crackfaang8 ай бұрын
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 :)
@hooriehmarefat59722 жыл бұрын
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.
@elikiperwasser2 ай бұрын
What about multiple multiplications like 2*3*4? The code you wrote will try to undo a summation but there isn't one..
@TheAdityabhandari2 жыл бұрын
If you're not using python3, the solution only gets accepted if you convert the res in the division to int(float(prev)/cur)
@crackfaang2 жыл бұрын
I mean this is a solution in Python 3 so it’s not guaranteed to work line by line in other languages
@n6i9k4a Жыл бұрын
@@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 Жыл бұрын
@@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
@akshaychavan55116 ай бұрын
You should just take the floor value of the division without complicating things.
@vishnuteja975818 күн бұрын
For division, if you want to truncate to 0, in python3 we would have to use int(float(prev) / curr)
@yingxu16946 күн бұрын
always do res -= prev when encountering * and /, what if the previous operation is -
@ye78412 жыл бұрын
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.
@crackfaang2 жыл бұрын
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 :)
@ye78412 жыл бұрын
@@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!
@wayne4591 Жыл бұрын
Really good explanation! Thanks! The idea of undo the previous operation the really good as well as the set prev = prev * cur one.
@dnm99312 ай бұрын
Thank you soo much! You are appreciated!
@darleisoares232 жыл бұрын
Great video, keep those coming, nice job
@crackfaang2 жыл бұрын
Thanks for the support! Make sure to subscribe so you don’t miss future videos
@symbol7672 жыл бұрын
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
@crackfaang2 жыл бұрын
Yea the stack solution is quite simple but the interview is basically always going to want the O(1) solution lol
@daxtermaster67938 ай бұрын
Thanks, it's true, it becomes intuitive when we understand first the stack solution.
@kapilrules26 күн бұрын
how did you assumed that curr_operation will always start with +? what if start with other operator?
@Andrew-dd2vf26 күн бұрын
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
@bellicose100x Жыл бұрын
great explanation of the intuition and solution.
@sarayarmohammadi33768 сағат бұрын
Thank you
@devinenivenkatesh6101 Жыл бұрын
Thanks you sir Nice solutions
@crackfaang Жыл бұрын
No problem glad you liked the video
@roywastaken Жыл бұрын
GOAT solution... i used a stack lol
@AmolGautam10 ай бұрын
Thank you so much.
@tobychow47618 ай бұрын
love this sol
@naaz26593 ай бұрын
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-dd2vf26 күн бұрын
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
@kalp25863 ай бұрын
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.
@crackfaang3 ай бұрын
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