Grokking Data Structures & Algorithms for Coding Interviews
0% completed
Solution: Sort Array by Increasing Frequency
.....
.....
.....
Like the course? Get enrolled and start learning!
A
Anonymous
· 2 years ago
In the solution, it implemented merge sort to sort by the frequency first and then by value (in descending order). Is there any reason why one can't implement a custom compare function which does the same thing? In an interview setting, it saves a lot more time and is less error prone.
In C++, I implemented something like this:
bool compare(const std::pair<int, int> &a, const std::pair<int, int> &b) { if (a.first != b.first) return a.first < b.first; else return a.second > b.second; }
Reading Progress
0%