Two pointers · Medium
A mobile client and server each provide record revisions sorted by ID. The merged snapshot must keep the newest revision for shared IDs.
Merge both lists without re-sorting and resolve equal-version conflicts in favor of the server.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ local:{id:integer,version:integer,value:string}[], server:{id:integer,version:integer,value:string}[] }
Input rules
- Each list has strictly increasing unique IDs.
What each field means
localandserver— record lists already sorted by increasing ID.- Each record has
id,versionandvalue; IDs are unique within each list.
Output
Return value and rules
- Return one merged record per ID, ordered by ID.
- For an ID on both sides, choose the greater version; equal versions choose the server record.
- Keep records appearing on only one side. Do not re-sort or mutate the input lists.
Constraints
- Each list has at most 100000 records
- IDs and versions are in [0,1000000000]; values are ASCII strings of at most 40 characters. Do not mutate input.
Examples
Example 1
{
"local": [
{
"id": 1,
"version": 1,
"value": "a"
}
],
"server": [
{
"id": 1,
"version": 2,
"value": "b"
}
]
}Returns
[
{
"id": 1,
"version": 2,
"value": "b"
}
]Example 2
{
"local": [],
"server": []
}Returns
[]Required complexity
O(n + m) time and O(n + m) output space