Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Custom comparison function

Jimmy

Sep 10, 2024

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; }

1

0

Comments
Comments
Em Eff
Em Eff8 months ago

The solution to this "easy" problem is overkill given the limited size of the data listed in the constraints

On this page