FunctionRacerProblemsRace

Postfix With Max and Min

EasyStacks and queuesPython, JavaScript, TypeScript, Java, C++

tokens is a postfix (reverse Polish) expression. Each token is either an integer written as a string, possibly negative like "-3", or one of the operators +, -, *, max and min.

An operator uses the two most recent values, the earlier one on the left: for - the result is left - right, and max and min give the larger and the smaller of the two. The expression is always valid and leaves exactly one value. Return that value.

Example 1

tokens ["3","4","max","2","*"]
returns 8

3 4 max is 4, then 4 2 * is 8.

Example 2

tokens ["5","1","2","+","-"]
returns 2

1 2 + is 3, then 5 3 - is 2.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Stacks and queues problems