First Unique Number Problem
Description
LeetCode Problem 1429.
You have a queue of integers, you need to retrieve the first unique integer in the queue.
Implement the FirstUnique class:
FirstUnique(int[] nums)Initializes the object with the numbers in the queue.int showFirstUnique()returns the value of the first unique integer of the queue, and returns -1 if there is no such integer.void add(int value)insert value to the queue.
Example 1:
Input:
["FirstUnique","showFirstUnique","add","showFirstUnique","add","showFirstUnique","add","showFirstUnique"]
[[[2,3,5]],[],[5],[],[2],[],[3],[]]
Output:
[null,2,null,2,null,3,null,-1]
Explanation:
FirstUnique firstUnique = new FirstUnique([2,3,5]);
firstUnique.showFirstUnique(); // return 2
firstUnique.add(5); // the queue is now [2,3,5,5]
firstUnique.showFirstUnique(); // return 2
firstUnique.add(2); // the queue is now [2,3,5,5,2]
firstUnique.showFirstUnique(); // return 3
firstUnique.add(3); // the queue is now [2,3,5,5,2,3]
firstUnique.showFirstUnique(); // return -1
Example 2:
Input:
["FirstUnique","showFirstUnique","add","add","add","add","add","showFirstUnique"]
[[[7,7,7,7,7,7]],[],[7],[3],[3],[7],[17],[]]
Output:
[null,-1,null,null,null,null,null,17]
Explanation:
FirstUnique firstUnique = new FirstUnique([7,7,7,7,7,7]);
firstUnique.showFirstUnique(); // return -1
firstUnique.add(7); // the queue is now [7,7,7,7,7,7,7]
firstUnique.add(3); // the queue is now [7,7,7,7,7,7,7,3]
firstUnique.add(3); // the queue is now [7,7,7,7,7,7,7,3,3]
firstUnique.add(7); // the queue is now [7,7,7,7,7,7,7,3,3,7]
firstUnique.add(17); // the queue is now [7,7,7,7,7,7,7,3,3,7,17]
firstUnique.showFirstUnique(); // return 17
Example 3:
Input:
["FirstUnique","showFirstUnique","add","showFirstUnique"]
[[[809]],[],[809],[]]
Output:
[null,809,null,-1]
Explanation:
FirstUnique firstUnique = new FirstUnique([809]);
firstUnique.showFirstUnique(); // return 809
firstUnique.add(809); // the queue is now [809,809]
firstUnique.showFirstUnique(); // return -1
Constraints:
1 <= nums.length <= 10^51 <= nums[i] <= 10^81 <= value <= 10^8- At most
50000calls will be made toshowFirstUniqueandadd.
Solution
Approach: Counter + Queue with Lazy Removal
Keep two things:
- a hash map
countwith how many times each value has been added, and - a queue with all values in the order they arrived.
add(value) increments count[value] and appends value to the queue. The constructor simply calls add for each number.
showFirstUnique() looks at the front of the queue. While the front value has a count other than 1, pop it. Then the front, if any, is the first unique value; otherwise return -1.
Popping is safe because counts only go up: a value that has been seen twice will never be unique again, so it never needs to come back. Duplicates are removed lazily, only when they reach the front, which keeps add trivial.
Complexity: add is O(1). showFirstUnique is O(1) amortized: every value is pushed once and popped at most once over all calls. O(n) space for all values added.
from collections import Counter, deque
class FirstUnique:
def __init__(self, nums: List[int]):
self.count = Counter(nums)
self.queue = deque(nums) # every value in arrival order; repeats are dropped lazily
def showFirstUnique(self) -> int:
# values at the front that are no longer unique never will be again, so drop them for good
while self.queue and self.count[self.queue[0]] != 1:
self.queue.popleft()
return self.queue[0] if self.queue else -1
def add(self, value: int) -> None:
self.count[value] += 1
self.queue.append(value)