This problem looks busy at first, but the accepted solution is built around one steady invariant. For 895. Maximum Frequency Stack, the solution in this repository is mainly a data structure design solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: data structure design, stack.
The notes already sitting in the source point us in the right direction:
- TLE
- 30 / 37 test cases passed.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are push, pop.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
- The stack stores unfinished context, which is usually the cleanest way to handle nested or monotonic structure.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//TLE
02//30 / 37 test cases passed.
03class FreqStack {
04public:
05 map<int, int> counter;
06 vector<int> vec;
07
08 FreqStack() {
09
10 }
11
12 void push(int x) {
13 vec.push_back(x);
14 counter[x]++;
15 }
16
17 int pop() {
18 map<int, int>::iterator p = max_element(counter.begin(), counter.end(),
19 [] (const pair<int, int> & p1, const pair<int, int> & p2) {
20 return p1.second < p2.second;
21 }
22 );
23 int mode = p->second;
24 int key;
25 for(int i = vec.size()-1; i >= 0; i--){
26 key = vec[i];
27 if(counter[key] == mode){
28 vec[i] = -1;
29 counter[key]--;
30 if(counter[key] == 0){
31 counter.erase(key);
32 }
33 break;
34 }
35 }
36 return key;
37 }
38};
39
40/**
41 * Your FreqStack object will be instantiated and called as such:
42 * FreqStack* obj = new FreqStack();
43 * obj->push(x);
44 * int param_2 = obj->pop();
45 */
46
47 //Runtime error
48 class FreqStack {
49public:
50 map<int, int> counter;
51 vector<int> vec;
52
53 FreqStack() {
54
55 }
56
57 void push(int x) {
58 vec.push_back(x);
59 counter[x]++;
60 }
61
62 int pop() {
63 map<int, int>::iterator p = max_element(counter.begin(), counter.end(),
64 [] (const pair<int, int> & p1, const pair<int, int> & p2) {
65 return p1.second < p2.second;
66 }
67 );
68 int mode = p->second;
69 int key;
70 auto rit = find_if(vec.rbegin(), vec.rend(),
71 [this, &mode](const int& key){
72 return this->counter[key] == mode;
73 });
74 int idx = vec.rend() - rit - 1;
75 vec[idx] = -1;
76 counter[key]--;
77
78 return key;
79 }
80};
81
82/**
83 * Your FreqStack object will be instantiated and called as such:
84 * FreqStack* obj = new FreqStack();
85 * obj->push(x);
86 * int param_2 = obj->pop();
87 */
88
89//Approach 1: Stack of Stacks
90//Runtime: 408 ms, faster than 10.44% of C++ online submissions for Maximum Frequency Stack.
91//Memory Usage: 73.1 MB, less than 91.67% of C++ online submissions for Maximum Frequency Stack.
92//time: push and pop, O(1), space: O(n)
93class FreqStack {
94public:
95 map<int, int> freq;
96 map<int, stack<int>> group;
97 int maxfreq;
98
99 FreqStack() {
100 maxfreq = 0;
101 }
102
103 void push(int x) {
104 int f = freq[x] + 1;
105 freq[x] = f;
106 maxfreq = max(maxfreq, f);
107
108 // don't need to check whether group[f] is initialized?
109 group[f].push(x);
110
111 /*
112 if x is pushed multiple times,
113 then group[1], group[2], group[3], ... will all have x
114 */
115 }
116
117 int pop() {
118 int x = group[maxfreq].top(); group[maxfreq].pop();
119 freq[x]--;
120 if(group[maxfreq].size() == 0){
121 /*
122 In this circumstance, maxfreq is consecutive
123 */
124 maxfreq--;
125 }
126 return x;
127 }
128};
129
130/**
131 * Your FreqStack object will be instantiated and called as such:
132 * FreqStack* obj = new FreqStack();
133 * obj->push(x);
134 * int param_2 = obj->pop();
135 */
Cost