Maximum Frequency Stack - Complete Solution Guide
Maximum Frequency Stack is LeetCode problem 895, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Design a stack-like data structure to push elements to the stack and pop the most frequent element from the stack. Implement the FreqStack class: FreqStack() constructs an empty frequency stack. void push(int val) pushes an integer val onto the top of the stack. int pop() removes and returns the most frequent element in the stack. If there is a tie for the most frequent element, the element closest to the stack's top is removed and returned. Example 1: Input ["FreqStack", "push", "push", "push",
Detailed Explanation
The problem requires designing a stack-like data structure called `FreqStack`. This data structure needs to support two operations: `push(val)` which adds an integer `val` to the top of the stack, and `pop()` which removes and returns the most frequent element in the stack. If multiple elements have the same highest frequency, the element closest to the top of the stack is returned.
Solution Approach
The solution uses two hashmaps and a `max_freq` variable. The `freq` hashmap stores the frequency of each element. The `group` hashmap stores a list of elements for each frequency level. When an element is pushed, its frequency is updated, and it is added to the corresponding list in `group`. The `max_freq` variable is updated whenever a new maximum frequency is encountered. When popping, the element from the top of the list associated with `max_freq` is removed, its frequency is decremented, and `max_freq` is decremented if the list becomes empty.
Step-by-Step Algorithm
- Step 1: Initialize `freq` (frequency hashmap), `group` (frequency-based stack hashmap), and `max_freq` to 0.
- Step 2: In `push(val)`: Increment `freq[val]`.
- Step 3: Update `max_freq` if `freq[val]` is greater than the current `max_freq`.
- Step 4: Append `val` to `group[freq[val]]` (the stack associated with its new frequency).
- Step 5: In `pop()`: Get the element at the top of `group[max_freq]` and remove it.
- Step 6: Decrement the frequency of the popped element in `freq`.
- Step 7: If `group[max_freq]` is now empty, decrement `max_freq`.
- Step 8: Return the popped element.
Key Insights
- Insight 1: A hashmap (or dictionary) is needed to efficiently track the frequency of each element.
- Insight 2: Another hashmap is needed to store elements grouped by their frequencies, with each frequency mapping to a stack of elements having that frequency.
- Insight 3: Maintain a `max_freq` variable to keep track of the highest frequency seen so far. This allows O(1) access to the stack containing elements with the maximum frequency.
Complexity Analysis
Time Complexity: O(1)
Space Complexity: O(N)
Topics
This problem involves: Hash Table, Stack, Design, Ordered Set.
Companies
Asked at: Nutanix, PayPal, Salesforce, Snap.