Reverse Polish Notation: Types of Mathematical Notations & Using A Stack To Solve RPN Expressions

  Рет қаралды 73,986

Back To Back SWE

Back To Back SWE

Күн бұрын

Free 5-Day Mini-Course: backtobackswe.com
Try Our Full Platform: backtobackswe....
📹 Intuitive Video Explanations
🏃 Run Code As You Learn
💾 Save Progress
❓New Unseen Questions
🔎 Get All Solutions
Question: Given an array with a sequence that represents a RPN expression, evaluate the Reverse Polish Notation expression.
This is one of those textbook problems. It is a classic expression of when LIFO behaviour is favorable to us when solving certain problems.
To have an RPN expression we need 2 things by definition:
A single digit or series of digits.
It is in the form ["A", "B", "o"] where A and B are integers and o is an operator (either +, -, *, or / ).
Examples:
[ "3", "4", "+", "2", "*", "1", "+" ]
is the same things as ( ( 3 + 4 ) * 2 ) + 1
which is the same things as ( 3 + 4 ) * 2 + 1 because of order of opeartions.
Example 2:
[ "1", "1", "+", "2", "2", "*", "+" ]
Approach
This is a classic stack problem, let us just do this.
The 2 key operations:
When we see a digit we push it to the stack.
When we see an operation we perform 2 pops, apply the operation between the 2 values (first popped item goes on left of the sign, 2nd popped item goes on the right of the sign), and then push the result back onto the stack so we can work with it as we continue.
If it is a valid RPN expression then we should have no problems with mismatches and null pointers, clarify that it is a valid RPN string always with your interviewer.
Complexities
n is the length of the RPN expression
Time: O( n )
We will process all n operators/operands in the expression. Each will either entail an O(1) push/pop or an O(1) arithmetic calculation.
Arithmetic operations take O(1).
Push and pop take O(1).
Space: O( d )
Let d be the total operands (numbers).
Let b be the number of operators (+, -, *, /)
If we have d digits and b operators where d + b = n, we certainly will not use O( d + b ) space (operators are not pushed to the stack).
A worst case is that we have all numbers, followed by the operators. And we will have a stack height of d digits. So we can bound to that.
++++++++++++++++++++++++++++++++++++++++++++++++++
HackerRank: / @hackerrankofficial
Tuschar Roy: / tusharroy2525
GeeksForGeeks: / @geeksforgeeksvideos
Jarvis Johnson: / vsympathyv
Success In Tech: / @successintech
++++++++++++++++++++++++++++++++++++++++++++++++++
This question is number 9.2 in "Elements of Programming Interviews" by Adnan Aziz, Tsung-Hsien Lee, and Amit Prakash.

Пікірлер: 128
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
Table of Contents Weird Flex But Ok (yeah...same shirt too) 0:00 - 0:02 The Problem Introduction 0:02 - 0:23 Different Expression Notations 0:23 - 2:05 Let's Look At Reverse Polish Notation 2:05 - 5:07 Walkthrough With A Stack 5:07 - 6:50 Time Complexity 6:50 - 7:36 Space Complexity 7:36 - 8:36 An abrupt ending because I forgot to do an outro 8:36 The code for this problem is in the description. Fully commented for teaching purposes.
@BakaDemi
@BakaDemi 2 жыл бұрын
where is the code in the description?
@simongrushka983
@simongrushka983 Жыл бұрын
as a pole I do apraciate that when you were talking about polish notation you put a proper polish flag but when you were talking about the reverse one you put it upside down :)
@eladyaniv767
@eladyaniv767 5 жыл бұрын
i dont know how ive found your channel, but thank you for this amazing content!
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
Welcome. You are appreciated here.
@janmichaelaustria620
@janmichaelaustria620 4 жыл бұрын
Thanks for the video my man! I just encountered this problem on LC and I had no idea how to evaluate RPN with pencil and paper.
@DeadManProp
@DeadManProp 4 жыл бұрын
I'm here because I'm Polish lol You're a great teacher. Thank you!
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
nice and thx
@hritwiksom3032
@hritwiksom3032 4 жыл бұрын
So it may sound stupid but I'm curious about one even though it's called polish notation, do you guys actually use it for calculations instead of infix in your day to day life?
@pseudounknow5559
@pseudounknow5559 3 жыл бұрын
@@hritwiksom3032 no we dont use it lol
@garrettstephens91
@garrettstephens91 2 жыл бұрын
Thanks for the video. I get in RPN how to add, subtract, multiply, and divide single digits together like 5+5 (RPN: 55+). How would you express two or more digits in RPN (like 22+34 or 302 +675)?
@kaagaj_bottle
@kaagaj_bottle Жыл бұрын
well one coult use array which would store each term in a expression and use the the array as a stack
@0LoneTech
@0LoneTech Жыл бұрын
You use a separator, e.g. space or Enter. Often the parser will interpret words, as in Forth, but you could also have single keystrokes working on the stack. I.e. 5 could mean if new entry then 5 else 10 * 5 + (the inner step in a left fold parser of decimal numbers). So simply "22 34 +" or "302 675 +" (the space before + is not needed in keystroke calculators).
@atanusaha8374
@atanusaha8374 3 жыл бұрын
Your teaching style is too good 👍 Take love from India 🇮🇳
@noapoleon_
@noapoleon_ 9 ай бұрын
Thank you very much :] Straight forward, easy to understand examples. Instantly helped me with a coding exercise I was doing
@krazzyfaceoffs
@krazzyfaceoffs 4 жыл бұрын
thanks for all this man...for these videos! i appreciate your efforts in teaching....stay blessed
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
thanks.
@nikhilarora7227
@nikhilarora7227 4 жыл бұрын
one of the best tutorials i have seen till date !!!. can you please suggest some good programming books and resources.
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
thx.
@dominiorrr6510
@dominiorrr6510 Жыл бұрын
This video made the RPN very easy to understand. On my lecture it was way less clear. Thank you, Mister.
@YourKidnay
@YourKidnay 8 ай бұрын
wait i love this guy lmfao hes such a good teacher
@Jason-tp5cb
@Jason-tp5cb 3 жыл бұрын
I visualize it like Tetris. The operands are regular blocks. The operators are 'bombs' that do a specific thing to the blocks below it.
@perseusz1691
@perseusz1691 Жыл бұрын
Finally i understand RNP now, thanks a lot!
@BackToBackSWE
@BackToBackSWE Жыл бұрын
Thank you! Please enjoy a special code from us - backtobackswe.com/checkout?plan=lifetime-legacy&discount_code=perseusz1691 🎉
@kueen3032
@kueen3032 5 жыл бұрын
Hi! I have seen your videos, they are really great, I found you from a Leetcode thread. My Google interview is scheduled for next month, could you please share your interview experience and give me some tips for the interview. On what topics should i focus more? I've been practicing questions on Leetcode, geeksforgeeks and i do follow your as well as Tushar Roy's videos.
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
Nice! and I guess all topics? not sure if I can narrow anything since it is really a personal question of what one should study. Just keep doing what you do and do many live interviews
@kueen3032
@kueen3032 5 жыл бұрын
@@BackToBackSWE thank you! yeah i'll do mock interviews.
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
@@kueen3032 great
@qaziyahya2818
@qaziyahya2818 4 жыл бұрын
how did it go? did you get the job?
@kueen3032
@kueen3032 4 жыл бұрын
@@qaziyahya2818 wasn't able to make through onsite rounds. They contacted me again few weeks ago, for the interviews as the cool down was of 6 months, but I declined politely saying I wasn't prepared enough. The recruiter told me I can apply next year again.
@isaiahelijah6572
@isaiahelijah6572 Жыл бұрын
I love your teaching so much
@latedeveloper7836
@latedeveloper7836 3 жыл бұрын
2:19 What you need to be able to read Reverse Polish Notation 3:30 Perform an operation as soon as you have sufficient operands and an operator 4:05 Text note about last seen items being the first out 4:30 Intro to this problem as a stack 5:10 Start of the stack problem 6:46 Time and space complexity for this expression
@Nurlanbai
@Nurlanbai Жыл бұрын
This video was immensely helpful. Thank you!
@BackToBackSWE
@BackToBackSWE Жыл бұрын
Happy Holidays! Really glad to help 🎉 Do you know about the BacktoBackSWE 5 Day Free Mini Course? Check it out here - backtobackswe.com/
@trumanhw
@trumanhw 3 жыл бұрын
Your style of communication (tone, cadence, & even your use of subtleties like pauses) is excellent. Linguistically..? I love a communicator who knows how and when to employ a [well-used] _pause._ Which, to me..? (and even generally..?) Is the operand-equivolent to reading words written that are 'bold & italicized.' A+ As in, if I were reading bold text I could raise my PITCH (not volume) and / or make it steccato ... But bold AND italic? That might require a well-timed pause preceding orrating those _written words._
@TheChristianmeza97
@TheChristianmeza97 5 жыл бұрын
Thank you very much for the explanation. Very thorough and clear. EDIT: Just realized all the cuts you did during editing. Very very useful skill and know how to keep viewers engaged in the video.
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
hahaha yeah, i guess
@MUmer-jg2uu
@MUmer-jg2uu 4 жыл бұрын
Ayo the guy explain better than the shit i learn in online classes
@dilushfernando9560
@dilushfernando9560 4 жыл бұрын
Got my A levels tomorrow, thanks man:)
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
sure
@ElfHimSelf
@ElfHimSelf 5 жыл бұрын
Do you have a list of all 250 problems you are going to cover?
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
yeah, but it is riddled with notes everywhere. I'd love to publish it but to be honest it is basically all very popular questions and famous algorithms.
@ElfHimSelf
@ElfHimSelf 5 жыл бұрын
@@BackToBackSWE Okay, cool. I'll keep doing popular leetcode problems and watching your videos :)
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
@@ElfHimSelf 👀👀🔥🔥
@ahcenebelhadi955
@ahcenebelhadi955 Жыл бұрын
amazing format ! thanks
@BackToBackSWE
@BackToBackSWE Жыл бұрын
Glad it was helpful! 😄 Also check out our FREE DSA Interview Prep Mini-Course - backtobackswe.com/ 🎉
@pixarfilmz4769
@pixarfilmz4769 3 жыл бұрын
Thanks, you explain extremely well
@muhammedyilmaz2907
@muhammedyilmaz2907 4 жыл бұрын
Very clear explaination.
@lukasznowarkiewicz
@lukasznowarkiewicz 4 жыл бұрын
Great explanation, thank you!
@bienlizardo6441
@bienlizardo6441 4 жыл бұрын
this video was so helpful!
@Alan-bu2hi
@Alan-bu2hi 4 ай бұрын
Wish I had found this in 2019 tears 😭
@جابرالسهرودي
@جابرالسهرودي 4 жыл бұрын
This is very helpful! Thx
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
sure
@antuha-cs4ie
@antuha-cs4ie Жыл бұрын
love from poland thank you for video
@BackToBackSWE
@BackToBackSWE Жыл бұрын
Thank you! Please enjoy a special code from us - backtobackswe.com/checkout?plan=lifetime-legacy&discount_code=antuha-cs4ie 🎉
@gilbert102
@gilbert102 4 жыл бұрын
Awesome work!! Thank you!!
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
sure
@orangeshoes
@orangeshoes 3 жыл бұрын
How would we solve (if possible) a variation of this problem where "+" and "*" can have any number of operands?
@bjornolsson9103
@bjornolsson9103 2 жыл бұрын
Great video, thank you very much! :)
@jimzhu7654
@jimzhu7654 4 жыл бұрын
You are the best!
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
no u
@郭梓梁
@郭梓梁 5 жыл бұрын
Nice explanation!
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
thx
@hey_you_123
@hey_you_123 3 жыл бұрын
How can I solve precedence operation using Reverse Polish Notation, if is possible?
@manishmendhekar1973
@manishmendhekar1973 4 жыл бұрын
Hello Would like to make videos on combination of Stack-Queue with Coding example means 1.Stack+Queue=Queue 2. Queue + Stack=Stack 3.Queue+Queue=Stack 4.Stack+stack=Queue I have faced questions in this combination would please make videos in this topic.
@natzeni1489
@natzeni1489 2 жыл бұрын
Thank you
@ShaliniNegi24
@ShaliniNegi24 4 жыл бұрын
Thank you!
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
sure
@sju9227
@sju9227 5 жыл бұрын
What if there are unary operators?
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
we assume only binary operators
@Retardsbeeverywhereb
@Retardsbeeverywhereb 5 жыл бұрын
@@BackToBackSWE what about ? : ternary operator
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
@@Retardsbeeverywhereb dang, jesus u got me
@ayushtiwari6815
@ayushtiwari6815 4 жыл бұрын
Where is link of code in discription I can't see
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
The repository is deprecated - we only maintain backtobackswe.com now.
@Mohamed_Hamada_
@Mohamed_Hamada_ Жыл бұрын
thanks so much, bro
@BackToBackSWE
@BackToBackSWE Жыл бұрын
Happy Holidays! Really glad to help 🎉 Do you know about the BacktoBackSWE 5 Day Free Mini Course? Check it out here - backtobackswe.com/
@quintonwilson8565
@quintonwilson8565 3 жыл бұрын
nice vid, helpful
@tinaukabi2394
@tinaukabi2394 3 жыл бұрын
I’m kind of confused about how the 2 was added to the first postfix 3 4+ Was the 3 and 4 counted to get the 2 or is 2 added generally?
@Bdixit
@Bdixit 3 жыл бұрын
2 is the next isdigit thing in the postfix string "34+2*1+"
@kela2812
@kela2812 4 жыл бұрын
What about if I have [22+1+1] then I cant do the operation with +, how can I keep my 22 until I get the other number Thanks
@a_commenter
@a_commenter 3 жыл бұрын
If I'm reading your question right, that would be written as 22 1 1 + +.
@aomafura3374
@aomafura3374 3 жыл бұрын
@@a_commenter It's actually 22 1 + 1 +
@a_commenter
@a_commenter 3 жыл бұрын
@@aomafura3374 They do the same thing, since addition is commutative.
@boriscreativespace
@boriscreativespace 4 жыл бұрын
good vid man
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
thx
@abdullateefadeniran-yusuf2214
@abdullateefadeniran-yusuf2214 Жыл бұрын
Thank you so very muchhhh
@BackToBackSWE
@BackToBackSWE Жыл бұрын
Really glad to help 🎉 Do you know about our 5-Day Free DSA Mini Course? Check it out here - backtobackswe.com/
@jobomathaha9015
@jobomathaha9015 2 жыл бұрын
Man YOu are so good thank you
@BackToBackSWE
@BackToBackSWE 2 жыл бұрын
Thank You, Glad you liked it. Do check out backtobackswe.com/platform/content and please recommend us to your family and friends :)
@panagiotiskyriakou3866
@panagiotiskyriakou3866 Ай бұрын
what about negative numbers ?!?
@rodrigofilho1996
@rodrigofilho1996 4 жыл бұрын
The thing about RPN, could it be scaled to be used on multiple CPU cores?
@0LoneTech
@0LoneTech Жыл бұрын
It describes a serial process, but there's nothing preventing rewriting that into a tree, finding independent branches, and distributing them to separate execution units. Modern CPUs do this with their machine language using register renaming and superscalar out of order execution. The key to parallelism is frequently using higher orders like SIMD or control flow refactoring. We might not have a meaningful parallel form of a small expression, but once you apply it to arrays of data we can partition work in e.g. map or reduce. Futhark is one language specialized in this.
@trumanhw
@trumanhw 3 жыл бұрын
I like that the RPN string / sentence defines the order of operation Execute the symbol's provided. My question though..? handling 2-digit integers that'd (thus far) be ambiguous, ie: 123+2* ... means ..? (12+3)(2) or (1+23)(2) are there space-symbols you can use? (and perhaps axiomatically -- always resolve implicit ambiguities.) Again, I REALLY like your style of communication, tone, and your PAUSES. Linguistically, I find pauses to be like making text 'BOLD or ITALICIZED' if typed.
@alltheusernamesaregone
@alltheusernamesaregone 2 жыл бұрын
ur a G bro!
@curtarmmar
@curtarmmar 2 жыл бұрын
I know this isn’t advanced math by any means but I don’t think I’ll ever be able to deviate from my infix upbringing
@sofiullah-iqbal-kiron
@sofiullah-iqbal-kiron 3 жыл бұрын
Love You.
@mukulmalviya1605
@mukulmalviya1605 5 жыл бұрын
but if you solve 3+4*2 the answer should be 11 ?do we need not to consider operator precedence
@mukulmalviya1605
@mukulmalviya1605 5 жыл бұрын
@@BackToBackSWE ok I understand
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
@@mukulmalviya1605 nice
@trumanhw
@trumanhw 3 жыл бұрын
Wait, it's Polish or polished ..? As in, the nation ..? (sincere) (btw, I really like your speaking / communication skills. I'd like to think we do it similarly). :) Thanks I'd just interject the phrase -- what does this CHANGE. :)
@0LoneTech
@0LoneTech Жыл бұрын
Yes, as in the nation; it was described by a Polish logician named Jan Łukasiewicz, and people aren't confident in remembering, spelling or pronouncing his name. Some languages that use prefix notation extend it to arbitrary arity (e.g. Lisp (+ 1 2 3)), while operations in RPN tend to use fixed arity (1 2 3 + + or 1 2 + 3 +). The use of fixed arity means we never need grouping (whether explicit through parenthesis or implicit through associativity and precedence). RPN is closer to computation order; any operation can only depend on what's to the left.
@enside8822
@enside8822 2 жыл бұрын
Noted king
@momotarodadumpling4065
@momotarodadumpling4065 3 жыл бұрын
yeah you can say 3D instead... 👍
@CamillaHodge-r9n
@CamillaHodge-r9n 2 ай бұрын
Blaise Radial
@esracigdem2714
@esracigdem2714 4 жыл бұрын
reverse flag joke is very funny, though
@BackToBackSWE
@BackToBackSWE 4 жыл бұрын
what, oh haha
@minju2871
@minju2871 2 жыл бұрын
교수님보다 잘 설명하시네요 ㅎㅎㅎ
@floatingfortress721
@floatingfortress721 2 жыл бұрын
operand operator operation operate operating
@MultiSilverbolt
@MultiSilverbolt 5 жыл бұрын
Thank fuck I found your channel
@BackToBackSWE
@BackToBackSWE 5 жыл бұрын
hahahahaaha
@normantuan56
@normantuan56 3 жыл бұрын
Ah yes the Reverse Polish notation, or what I'd like to call the Indonesian notation
@eloeloeloelo8401
@eloeloeloelo8401 2 жыл бұрын
Goodbamboo
@GlendaPhillips-f8r
@GlendaPhillips-f8r 3 ай бұрын
Davis Brian Jones Shirley Wilson Linda
@DrMuzis
@DrMuzis 2 жыл бұрын
where the fuck is the code
@BackToBackSWE
@BackToBackSWE 2 жыл бұрын
Hi, the code is managed on backtobackswe.com/
@andige639
@andige639 Жыл бұрын
Petition to rename Reverse Polish Notation as Indonesian Notation
@GriffithPillow
@GriffithPillow 11 ай бұрын
data structures prime newton
@thomasspinthakis2165
@thomasspinthakis2165 3 жыл бұрын
thanks for the video ,awesome work!
Add Two Numbers Without The "+" Sign (Bit Shifting Basics)
18:25
Back To Back SWE
Рет қаралды 125 М.
When Cucumbers Meet PVC Pipe The Results Are Wild! 🤭
00:44
Crafty Buddy
Рет қаралды 60 МЛН
Муж внезапно вернулся домой @Oscar_elteacher
00:43
История одного вокалиста
Рет қаралды 7 МЛН
If people acted like cats 🙀😹 LeoNata family #shorts
00:22
LeoNata Family
Рет қаралды 23 МЛН
Search A 2D Sorted Matrix - Fundamentals of Search Space Reduction
29:31
Back To Back SWE
Рет қаралды 51 М.
Reverse Polish Notation and The Stack - Computerphile
13:32
Computerphile
Рет қаралды 308 М.
The Joys of RPN
10:37
MathWithoutBorders
Рет қаралды 116 М.
Reverse Polish Grows on Trees - Computerphile
9:51
Computerphile
Рет қаралды 94 М.
Imaginary numbers aren't imaginary
13:55
Ali the Dazzling
Рет қаралды 122 М.
The Oldest Unsolved Problem in Math
31:33
Veritasium
Рет қаралды 12 МЛН
When Cucumbers Meet PVC Pipe The Results Are Wild! 🤭
00:44
Crafty Buddy
Рет қаралды 60 МЛН