Sorting · Medium
A client merges API feed records before slicing a page. Retries may repeat IDs with newer timestamps.
Deduplicate by ID, keep the latest timestamp, sort newest first then smaller numeric ID, and paginate the full set.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ records:{id:integer,time:integer}[], offset:integer, limit:integer }
What each field means
records— feed records with numericidandtime; the same ID may occur more than once.offset— number of unique sorted records to skip.limit— maximum number of IDs to include in the page.
Output
Return value and rules
- Keep the greatest timestamp for each ID, then sort by timestamp descending and ID ascending.
- Return
{total, ids}:totalis the full unique-record count;idsis the requested page. - An exhausted page or zero limit returns empty
idswithout changingtotal.
Constraints
- 0 <= records.length <= 100000; offset in [0,1000000000]; limit in [0,100000]
- IDs and times in [0,1000000000]. Repeated identical records are harmless.
Examples
Example 1
{
"limit": 1,
"offset": 0,
"records": [
{
"id": 2,
"time": 1
},
{
"id": 1,
"time": 3
},
{
"id": 2,
"time": 4
}
]
}Returns
{
"total": 2,
"ids": [
2
]
}Example 2
{
"limit": 1,
"offset": 0,
"records": []
}Returns
{
"total": 0,
"ids": []
}Required complexity
O(n + u log u) time and O(u) space