Stack
Key points
ArrayDeque
Front(First) [6, 5, 4, 1, 2, 3] Rear(Last)
Stack push to Front and pop from Front
Queue offer to Rear and poll from Front
Queue offerFirst to Front, pollLast from Rear
Example:
Calculator / evaluate expression
Track at least 3 variables: num1, op1, currNum, {currOp}, {num3} ...
currOp is +/-, or op1 && currOp both are * or /: currNum = eval(num1, op1, currNum)
other cases: currNum = eval(currNum, currOp, num3). Because we don't know whether it is left parenthesis or num3 after currOp, let's push currNum, currOp to stack and do calculation later.
Monotonic Queue problem
Maintain increasing or decreasing sequence.
Lexicographical order
Find a way to fast forward:
Select largest/smallest k number from array
Last updated
Was this helpful?