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:

Deque<Integer> q = new ArrayDeque<>();
q.offer(1);
q.offer(2);
q.offer(3);
q.offerFirst(4);
q.offerFirst(5);
q.pollLast();
q.push(7);
q.push(8);
q.push(9);
q.pop();
q.toString(); // [8, 7, 5, 4, 1, 2]

Calculator / evaluate expression

Track at least 3 variables: num1, op1, currNum, {currOp}, {num3} ...

  1. currOp is +/-, or op1 && currOp both are * or /: currNum = eval(num1, op1, currNum)

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

https://1e9.medium.com/monotonic-queue-notes-980a019d5793

Lexicographical order

Find a way to fast forward:

Select largest/smallest k number from array

Last updated