← 返回 amazon 的题目列表First Unique Visitor ID Retrieval with O(1) Query
类型:online_judge
amazon
Assume there is a website where people come to visit. Please design and implement two functions:
record_visit(person_id: int) -> None: Record the ID of each person visiting the site.
find_first_visitor() -> int: Find the ID of the first person who visited the website only once. If there is no such user, return -1.
Requirements:
The record_visit function may be called a very large number of times n, up to 10^5.
The find_first_visitor function should complete in almost O(1) time.
Example
Suppose record_visit is called in sequence as follows:
record_visit(2)
record_visit(3)
record_visit(3)
record_visit(4)
record_visit(5)
Calling find_first_visitor() should return 2, as 2 is the only person who visited the site and did so only once.
Constraints
User IDs are integers and positive. All inputs are stored in memory; persistence is not required.
Example
Input
print(find_first_visitor())