# Frequency Stack with Recency Tie-Breaking
Implement `frequency_stack(operations: list[list[int]]) -> list[int]`.
Each operation is one of the following:
- `[1, x]`: push integer `x` onto the stack.
- `[2]`: pop and return the value with the highest current frequency. If several values have the same frequency, pop the one whose most recent remaining push occurred latest.
Return the values produced by pop operations, in operation order.
## Valid Input Domain
- Every operation is exactly `[1, x]` or `[2]`.
- A pop operation is issued only when at least one value is present.
- Values are signed 32-bit integers.
## Constraints
- `1 <= operations.length <= 200,000`
- The result must follow the frequency and recency rules exactly.
## Public Examples
### Example 1
Input: `[[1, 5], [1, 7], [1, 5], [2], [2]]`
Output: `[5, 7]`
The first pop chooses `5` because it has frequency two. The second pop chooses `7` because both remaining values have frequency one and `7` was pushed more recently.
### Example 2
Input: `[[1, 4], [1, 4], [1, 8], [1, 8], [2], [2], [2], [2]]`
Output: `[8, 4, 8, 4]`
```hint Track frequency and recency
Think about what information must be updated after every push and pop without scanning all stored values.
```
Quick Answer: Implement a frequency stack whose pop operation returns the most frequent value and breaks frequency ties by recency.
[2]
: pop and return the value with the highest current frequency. If several values have the same frequency, pop the one whose most recent remaining push occurred latest.
Return the values produced by pop operations, in operation order.
Valid Input Domain
Every operation is exactly
[1, x]
or
[2]
.
A pop operation is issued only when at least one value is present.
Values are signed 32-bit integers.
Constraints
1 <= operations.length <= 200,000
The result must follow the frequency and recency rules exactly.
Public Examples
Example 1
Input: [[1, 5], [1, 7], [1, 5], [2], [2]]
Output: [5, 7]
The first pop chooses 5 because it has frequency two. The second pop chooses 7 because both remaining values have frequency one and 7 was pushed more recently.