Problem · Array
Top-K URLs Overall and in the Last 24 Hours
Learn this problemProblem statement
You are given access-log records in parallel arrays. Record i contains URL urls[i] and Unix-second timestamp timestamps[i].
Return two ranked URL lists:
- the top
kURLs across all records; - the top
kURLs whose timestamps are in the inclusive interval[queryTime - 86400, queryTime].
Rank a URL with more accesses first. Break equal-frequency ties by lexicographically smaller URL. If a group contains fewer than k distinct URLs, return all of them. The result is a two-row string matrix in overall-then-recent order.
Function
topKUrls(urls: String[], timestamps: long[], queryTime: long, k: int) → String[][]Examples
Example 1
urls = ["old","old","old","new","new","edge"]timestamps = [1,2,3,200000,199999,113600]queryTime = 200000k = 2return = [["old","new"],["new","edge"]]Overall, old appears three times and new twice. Only new and edge fall in the inclusive recent window.
Example 2
urls = ["b","a","c"]timestamps = [10,10,10]queryTime = 10k = 5return = [["a","b","c"],["a","b","c"]]All frequencies tie, so lexicographic order decides; fewer than k distinct URLs are returned.
Constraints
1 <= urls.length = timestamps.length <= 2 * 10^51 <= k <= 10^5- Each URL is a non-empty lowercase path of length at most
100. 0 <= timestamps[i] <= queryTime <= 10^12