Hash map · Medium
An API dashboard ranks routes by traffic count. Equal counts must produce a stable report across processes.
Return at most k route names sorted by decreasing frequency then ascending ASCII name.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ routes:string[], k:integer }
Input rules
- Route strings are nonempty ASCII paths.
What each field means
routes— one route string per request, including repeated requests.k— maximum number of distinct route names to return.
Output
Return value and rules
- Return up to
kdistinct routes. - Order them by request count descending; break equal-count ties by ASCII route name ascending.
- Return
[]whenkis zero or there are no requests.
Constraints
- 0 <= routes.length <= 100000; 0 <= k <= 100000
- Route names have at most 80 characters; ordering is not locale-dependent.
Examples
Example 1
{
"k": 2,
"routes": [
"/b",
"/a",
"/b",
"/a",
"/c"
]
}Returns
[
"/a",
"/b"
]Example 2
{
"k": 5,
"routes": []
}Returns
[]Required complexity
O(n + u log u) time and O(u) space