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

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 ก.พ. 2019
  • 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.
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 124

  • @BackToBackSWE
    @BackToBackSWE  5 ปีที่แล้ว +13

    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 ปีที่แล้ว +7

      where is the code in the description?

  • @janmichaelaustria620
    @janmichaelaustria620 3 ปีที่แล้ว +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.

  • @simongrushka983
    @simongrushka983 8 หลายเดือนก่อน +3

    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 ปีที่แล้ว +7

    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 ปีที่แล้ว +6

    thanks for all this man...for these videos! i appreciate your efforts in teaching....stay blessed

  • @noapoleon_
    @noapoleon_ 4 หลายเดือนก่อน

    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!

  • @pixarfilmz4769
    @pixarfilmz4769 3 ปีที่แล้ว

    Thanks, you explain extremely well

  • @nikhilarora7227
    @nikhilarora7227 4 ปีที่แล้ว +3

    one of the best tutorials i have seen till date !!!. can you please suggest some good programming books and resources.

  • @dominiorrr6510
    @dominiorrr6510 ปีที่แล้ว

    This video made the RPN very easy to understand. On my lecture it was way less clear. Thank you, Mister.

  • @gilbert102
    @gilbert102 4 ปีที่แล้ว +1

    Awesome work!! Thank you!!

  • @atanusaha8374
    @atanusaha8374 3 ปีที่แล้ว

    Your teaching style is too good 👍
    Take love from India 🇮🇳

  • @thomasspinthakis2165
    @thomasspinthakis2165 2 ปีที่แล้ว

    thanks for the video ,awesome work!

  • @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/

  • @bienlizardo6441
    @bienlizardo6441 3 ปีที่แล้ว

    this video was so helpful!

  • @YourKidnay
    @YourKidnay 4 หลายเดือนก่อน

    wait i love this guy lmfao hes such a good teacher

  • @muhammedyilmaz2907
    @muhammedyilmaz2907 3 ปีที่แล้ว

    Very clear explaination.

  • @bjornolsson9103
    @bjornolsson9103 ปีที่แล้ว

    Great video, thank you very much! :)

  • @isaiahelijah6572
    @isaiahelijah6572 8 หลายเดือนก่อน

    I love your teaching so much

  • @ahcenebelhadi955
    @ahcenebelhadi955 ปีที่แล้ว

    amazing format ! thanks

    • @BackToBackSWE
      @BackToBackSWE  ปีที่แล้ว

      Glad it was helpful! 😄 Also check out our FREE DSA Interview Prep Mini-Course - backtobackswe.com/ 🎉

  • @user-qy9ys7ux6v
    @user-qy9ys7ux6v 4 ปีที่แล้ว +1

    This is very helpful! Thx

  • @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 🎉

  • @user-xe5qv4ii9p
    @user-xe5qv4ii9p 4 ปีที่แล้ว +1

    Nice explanation!

  • @natzeni1489
    @natzeni1489 ปีที่แล้ว

    Thank you

  • @ShaliniNegi24
    @ShaliniNegi24 4 ปีที่แล้ว +1

    Thank you!

  • @jimzhu7654
    @jimzhu7654 4 ปีที่แล้ว +1

    You are the best!

  • @DeadManProp
    @DeadManProp 3 ปีที่แล้ว +2

    I'm here because I'm Polish lol You're a great teacher. Thank you!

    • @BackToBackSWE
      @BackToBackSWE  3 ปีที่แล้ว +1

      nice and thx

    • @hritwiksom3032
      @hritwiksom3032 3 ปีที่แล้ว +1

      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 ปีที่แล้ว +2

      @@hritwiksom3032 no we dont use it lol

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

  • @garrettstephens91
    @garrettstephens91 2 ปีที่แล้ว +6

    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 8 หลายเดือนก่อน

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

  • @quintonwilson8565
    @quintonwilson8565 3 ปีที่แล้ว

    nice vid, helpful

  • @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 🎉

  • @orangeshoes
    @orangeshoes 3 ปีที่แล้ว

    How would we solve (if possible) a variation of this problem where "+" and "*" can have any number of operands?

  • @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/

  • @TheChristianmeza97
    @TheChristianmeza97 4 ปีที่แล้ว +1

    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.

  • @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/

  • @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 :)

  • @sofiullahiqbalkiron6818
    @sofiullahiqbalkiron6818 3 ปีที่แล้ว

    Love You.

  • @hey_you_123
    @hey_you_123 3 ปีที่แล้ว

    How can I solve precedence operation using Reverse Polish Notation, if is possible?

  • @dilushfernando9560
    @dilushfernando9560 3 ปีที่แล้ว +3

    Got my A levels tomorrow, thanks man:)

  • @kueen3032
    @kueen3032 4 ปีที่แล้ว +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 ปีที่แล้ว +3

      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 ปีที่แล้ว +2

      @@BackToBackSWE thank you! yeah i'll do mock interviews.

    • @BackToBackSWE
      @BackToBackSWE  4 ปีที่แล้ว +1

      @@kueen3032 great

    • @qaziyahya2818
      @qaziyahya2818 4 ปีที่แล้ว

      how did it go? did you get the job?

    • @kueen3032
      @kueen3032 4 ปีที่แล้ว +1

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

  • @boriscreativespace
    @boriscreativespace 3 ปีที่แล้ว +1

    good vid man

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

  • @alltheusernamesaregone
    @alltheusernamesaregone ปีที่แล้ว

    ur a G bro!

  • @MUmer-jg2uu
    @MUmer-jg2uu 3 ปีที่แล้ว

    Ayo the guy explain better than the shit i learn in online classes

  • @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 8 หลายเดือนก่อน +1

      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.

  • @kela2812
    @kela2812 3 ปีที่แล้ว +1

    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.

  • @ElfHimSelf
    @ElfHimSelf 5 ปีที่แล้ว +2

    Do you have a list of all 250 problems you are going to cover?

    • @BackToBackSWE
      @BackToBackSWE  5 ปีที่แล้ว +3

      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 ปีที่แล้ว +2

      @@BackToBackSWE Okay, cool. I'll keep doing popular leetcode problems and watching your videos :)

    • @BackToBackSWE
      @BackToBackSWE  5 ปีที่แล้ว

      @@ElfHimSelf 👀👀🔥🔥

  • @ayushtiwari6815
    @ayushtiwari6815 4 ปีที่แล้ว +1

    Where is link of code in discription
    I can't see

    • @BackToBackSWE
      @BackToBackSWE  4 ปีที่แล้ว

      The repository is deprecated - we only maintain backtobackswe.com now.

  • @enside8822
    @enside8822 ปีที่แล้ว

    Noted king

  • @mukulmalviya1605
    @mukulmalviya1605 4 ปีที่แล้ว

    but if you solve 3+4*2 the answer should be 11 ?do we need not to consider operator precedence

  • @sju9227
    @sju9227 4 ปีที่แล้ว +3

    What if there are unary operators?

    • @BackToBackSWE
      @BackToBackSWE  4 ปีที่แล้ว

      we assume only binary operators

    • @Retardsbeeverywhereb
      @Retardsbeeverywhereb 4 ปีที่แล้ว +1

      @@BackToBackSWE what about ? : ternary operator

    • @BackToBackSWE
      @BackToBackSWE  4 ปีที่แล้ว

      @@Retardsbeeverywhereb dang, jesus u got me

  • @trumanhw
    @trumanhw 2 ปีที่แล้ว +1

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

  • @floatingfortress721
    @floatingfortress721 2 ปีที่แล้ว

    operand operator operation operate operating

  • @minju2871
    @minju2871 ปีที่แล้ว

    교수님보다 잘 설명하시네요 ㅎㅎㅎ

  • @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 8 หลายเดือนก่อน

      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.

  • @MultiSilverbolt
    @MultiSilverbolt 4 ปีที่แล้ว +1

    Thank fuck I found your channel

  • @curtarmmar
    @curtarmmar ปีที่แล้ว

    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

  • @latedeveloper7836
    @latedeveloper7836 3 ปีที่แล้ว +1

    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

  • @eloeloeloelo8401
    @eloeloeloelo8401 ปีที่แล้ว

    Goodbamboo

  • @momotarodadumpling4065
    @momotarodadumpling4065 2 ปีที่แล้ว +1

    yeah you can say 3D instead... 👍

  • @esracigdem2714
    @esracigdem2714 4 ปีที่แล้ว +3

    reverse flag joke is very funny, though

  • @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 ปีที่แล้ว +1

    where the fuck is the code

    • @BackToBackSWE
      @BackToBackSWE  ปีที่แล้ว

      Hi, the code is managed on backtobackswe.com/

  • @andige639
    @andige639 10 หลายเดือนก่อน

    Petition to rename Reverse Polish Notation as Indonesian Notation

  • @GriffithPillow
    @GriffithPillow 7 หลายเดือนก่อน

    data structures prime newton