Skip to content

Commit

Permalink
feat(design,map,stack): Add 895
Browse files Browse the repository at this point in the history
  • Loading branch information
euchangxian committed Nov 12, 2024
1 parent 83baa04 commit 345f90f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
73 changes: 73 additions & 0 deletions C++/0895-MaximumFrequencyStack/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <algorithm>
#include <cstddef>
#include <cstdlib>
#include <unordered_map>
#include <vector>

/**
* Immediately, seems like an Ordered Set, ordered by frequency.
* But how to deal with the LIFO behaviour?
*
* Probably another Ordered Set, ordered by insertion order. Or rather, use
* a tuple {frequency, insertionOrder, value}. But this does not preserve the
* insertion order of multiple same-values.
*
* What about something like LFU? Keep track of the highest frequency.
*
* NOTE: Tapped out...weakkkkkk
* Visualize as Groups of Frequencies.
* When pushing an element, update its frequency with a Map.
* Using another Map<Frequency, Stack>, push the element onto the respective
* frequency's Stack.
*
* Optimization: Instead of using an Ordered Map/Set to retrieve the maximum
* frequency, an unordered Map, along with a variable to keep track of the
* current maximum frequency can be used.
* This works, because suppose our current maxFrequency is 3. Then, after
* popping this element, either there are other elements with frequency=3, or
* we can simply decrement the maxFrequency to 2. The element MUST have occurred
* at 2 times, and is in the frequency=2 group too.
*/
class FreqStack {
private:
int maxFreq{0};

// {val, freq}
std::unordered_map<int, int> frequency;

// {freq, stack}
std::unordered_map<int, std::vector<int>> groups;

public:
FreqStack() {}

void push(int val) {
int freq = ++frequency[val];
groups[freq].push_back(val);

maxFreq = std::max(maxFreq, freq);
}

int pop() {
std::vector<int>& maxFreqStack = groups[maxFreq];

int val = maxFreqStack.back();
maxFreqStack.pop_back();

if (--frequency[val] == 0) {
frequency.erase(val);
}

if (maxFreqStack.empty()) {
groups.erase(maxFreq--);
}
return val;
}
};

/**
* Your FreqStack object will be instantiated and called as such:
* FreqStack* obj = new FreqStack();
* obj->push(val);
* int param_2 = obj->pop();
*/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ Now solving in C++. Like it.
| 884 | UncommonWordsFromTwoSentences | [![C++](assets/c++.svg)](C++/0884-UncommonWordsFromTwoSentences/Solution.cpp) |
| 885 | SpiralMatrixThree | [![C++](assets/c++.svg)](C++/0885-SpiralMatrixThree/Solution.cpp) |
| 887 | SuperEggDrop | [![C++](assets/c++.svg)](C++/0887-SuperEggDrop/Solution.cpp) |
| 895 | MaximumFrequencyStack | [![C++](assets/c++.svg)](C++/0895-MaximumFrequencyStack/Solution.cpp) |
| 912 | SortAnArray | [![C++](assets/c++.svg)](C++/0912-SortAnArray/Solution.cpp) |
| 921 | MinimumAddToMakeParenthesesValid | [![C++](assets/c++.svg)](C++/0921-MinimumAddToMakeParenthesesValid/Solution.cpp) |
| 926 | FlipStringToMonotoneIncreasing | [![C++](assets/c++.svg)](C++/0926-FlipStringToMonotoneIncreasing/Solution.cpp) |
Expand Down

0 comments on commit 345f90f

Please sign in to comment.