Тёмный

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

Back To Back SWE
Подписаться 240 тыс.
Просмотров 72 тыс.
50% 1

Free 5-Day Mini-Course: backtobackswe.com
Try Our Full Platform: backtobackswe.com/pricing
📹 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.

Наука

Опубликовано:

 

24 фев 2019

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 125   
@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?
@janmichaelaustria620
@janmichaelaustria620 3 года назад
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.
@simongrushka983
@simongrushka983 9 месяцев назад
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.
@aadeshsrivastava6517
@aadeshsrivastava6517 4 года назад
thanks for all this man...for these videos! i appreciate your efforts in teaching....stay blessed
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks.
@noapoleon_
@noapoleon_ 5 месяцев назад
Thank you very much :] Straight forward, easy to understand examples. Instantly helped me with a coding exercise I was doing
@lukasznowarkiewicz
@lukasznowarkiewicz 3 года назад
Great explanation, thank you!
@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.
@thomasspinthakis2165
@thomasspinthakis2165 2 года назад
thanks for the video ,awesome work!
@gilbert102
@gilbert102 4 года назад
Awesome work!! Thank you!!
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@pixarfilmz4769
@pixarfilmz4769 3 года назад
Thanks, you explain extremely well
@bienlizardo6441
@bienlizardo6441 3 года назад
this video was so helpful!
@atanusaha8374
@atanusaha8374 3 года назад
Your teaching style is too good 👍 Take love from India 🇮🇳
@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/
@user-qy9ys7ux6v
@user-qy9ys7ux6v 4 года назад
This is very helpful! Thx
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@bjornolsson9103
@bjornolsson9103 Год назад
Great video, thank you very much! :)
@ahcenebelhadi955
@ahcenebelhadi955 Год назад
amazing format ! thanks
@BackToBackSWE
@BackToBackSWE Год назад
Glad it was helpful! 😄 Also check out our FREE DSA Interview Prep Mini-Course - backtobackswe.com/ 🎉
@muhammedyilmaz2907
@muhammedyilmaz2907 3 года назад
Very clear explaination.
@dominiorrr6510
@dominiorrr6510 Год назад
This video made the RPN very easy to understand. On my lecture it was way less clear. Thank you, Mister.
@isaiahelijah6572
@isaiahelijah6572 9 месяцев назад
I love your teaching so much
@user-xe5qv4ii9p
@user-xe5qv4ii9p 4 года назад
Nice explanation!
@BackToBackSWE
@BackToBackSWE 4 года назад
thx
@YourKidnay
@YourKidnay 5 месяцев назад
wait i love this guy lmfao hes such a good teacher
@ShaliniNegi24
@ShaliniNegi24 4 года назад
Thank you!
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@jimzhu7654
@jimzhu7654 4 года назад
You are the best!
@BackToBackSWE
@BackToBackSWE 4 года назад
no u
@quintonwilson8565
@quintonwilson8565 3 года назад
nice vid, helpful
@natzeni1489
@natzeni1489 Год назад
Thank you
@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/
@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/
@DeadManProp
@DeadManProp 3 года назад
I'm here because I'm Polish lol You're a great teacher. Thank you!
@BackToBackSWE
@BackToBackSWE 3 года назад
nice and thx
@hritwiksom3032
@hritwiksom3032 3 года назад
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
@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 🎉
@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 :)
@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 🎉
@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 9 месяцев назад
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).
@sofiullahiqbalkiron6818
@sofiullahiqbalkiron6818 3 года назад
Love You.
@boriscreativespace
@boriscreativespace 3 года назад
good vid man
@BackToBackSWE
@BackToBackSWE 3 года назад
thx
@TheChristianmeza97
@TheChristianmeza97 4 года назад
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 4 года назад
hahaha yeah, i guess
@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.
@alltheusernamesaregone
@alltheusernamesaregone Год назад
ur a G bro!
@kueen3032
@kueen3032 4 года назад
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 4 года назад
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 4 года назад
@@BackToBackSWE thank you! yeah i'll do mock interviews.
@BackToBackSWE
@BackToBackSWE 4 года назад
@@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.
@orangeshoes
@orangeshoes 3 года назад
How would we solve (if possible) a variation of this problem where "+" and "*" can have any number of operands?
@Alan-bu2hi
@Alan-bu2hi 16 дней назад
Wish I had found this in 2019 tears 😭
@dilushfernando9560
@dilushfernando9560 3 года назад
Got my A levels tomorrow, thanks man:)
@BackToBackSWE
@BackToBackSWE 3 года назад
sure
@hey_you_123
@hey_you_123 3 года назад
How can I solve precedence operation using Reverse Polish Notation, if is possible?
@enside8822
@enside8822 Год назад
Noted king
@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+"
@rodrigofilho1996
@rodrigofilho1996 3 года назад
The thing about RPN, could it be scaled to be used on multiple CPU cores?
@0LoneTech
@0LoneTech 9 месяцев назад
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.
@manishmendhekar1973
@manishmendhekar1973 3 года назад
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.
@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 👀👀🔥🔥
@MUmer-jg2uu
@MUmer-jg2uu 3 года назад
Ayo the guy explain better than the shit i learn in online classes
@kela2812
@kela2812 3 года назад
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.
@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.
@sju9227
@sju9227 4 года назад
What if there are unary operators?
@BackToBackSWE
@BackToBackSWE 4 года назад
we assume only binary operators
@Retardsbeeverywhereb
@Retardsbeeverywhereb 4 года назад
@@BackToBackSWE what about ? : ternary operator
@BackToBackSWE
@BackToBackSWE 4 года назад
@@Retardsbeeverywhereb dang, jesus u got me
@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
@MultiSilverbolt
@MultiSilverbolt 5 лет назад
Thank fuck I found your channel
@BackToBackSWE
@BackToBackSWE 5 лет назад
hahahahaaha
@floatingfortress721
@floatingfortress721 2 года назад
operand operator operation operate operating
@trumanhw
@trumanhw 2 года назад
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._
@trumanhw
@trumanhw 2 года назад
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 9 месяцев назад
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.
@minju2871
@minju2871 Год назад
교수님보다 잘 설명하시네요 ㅎㅎㅎ
@eloeloeloelo8401
@eloeloeloelo8401 Год назад
Goodbamboo
@esracigdem2714
@esracigdem2714 4 года назад
reverse flag joke is very funny, though
@BackToBackSWE
@BackToBackSWE 4 года назад
what, oh haha
@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
@momotarodadumpling4065
@momotarodadumpling4065 3 года назад
yeah you can say 3D instead... 👍
@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
@trumanhw
@trumanhw 2 года назад
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.
@normantuan56
@normantuan56 3 года назад
Ah yes the Reverse Polish notation, or what I'd like to call the Indonesian notation
@DrMuzis
@DrMuzis Год назад
where the fuck is the code
@BackToBackSWE
@BackToBackSWE Год назад
Hi, the code is managed on backtobackswe.com/
@andige639
@andige639 11 месяцев назад
Petition to rename Reverse Polish Notation as Indonesian Notation
@GriffithPillow
@GriffithPillow 8 месяцев назад
data structures prime newton
Далее
Reverse Polish Notation and The Stack - Computerphile
13:32
ОНИ НИКОГДА НЕ СПЯТ
28:35
Просмотров 1,1 млн
The Joys of RPN
10:37
Просмотров 114 тыс.
The SAT Question Everyone Got Wrong
18:25
Просмотров 12 млн
A Prime Surprise (Mertens Conjecture) - Numberphile
9:56
Infix to reverse polish using a stack
8:59
Просмотров 64 тыс.
Why there are no 3D complex numbers
15:21
Просмотров 69 тыс.
Fast Inverse Square Root - A Quake III Algorithm
20:08
КРАХ WINDOWS 19 ИЮЛЯ 2024 | ОБЪЯСНЯЕМ
10:04
Открываем домофоны с Mi Band 9
0:59
Просмотров 182 тыс.
iPhone 14 китайский сборка!
1:00
Просмотров 329 тыс.
Это iPhone 16
0:52
Просмотров 2,5 млн