Hash set · Easy
A search UI knows the total number of result pages and the page numbers already cached. It needs the missing pages in navigation order.
Return page numbers from 1 through total that have not been cached.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ total:integer, cached:integer[] }
Input rules
- Duplicate cached numbers are allowed; values outside [1,total] are ignored.
What each field means
total— number of result pages, numbered from1throughtotal.cached— page numbers already stored locally; duplicates and out-of-range values may occur.
Output
Return value and rules
- Return every valid page number missing from the cache, in increasing order.
- Ignore duplicates and cached numbers outside
1..total. - Return
[]if no valid page is missing.
Constraints
- 0 <= total <= 100000; 0 <= cached.length <= 100000
- Cached values are integers in [-1000000,1000000].
Examples
Example 1
{
"cached": [
4,
2,
2,
0
],
"total": 5
}Returns
[
1,
3,
5
]Example 2
{
"cached": [],
"total": 0
}Returns
[]Required complexity
O(total + cached.length) time and O(total + cached.length) space