-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1429_first_unique_number.py
More file actions
30 lines (23 loc) · 940 Bytes
/
Copy path1429_first_unique_number.py
File metadata and controls
30 lines (23 loc) · 940 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from typing import List
from collections import List
class FirstUnique:
def __init__(self, nums: List[int]):
self.q = deque(nums)
self.counts = Counter(nums)
self.pointer = None
for i in range(len(self.q)):
if self.counts[self.q[i]] == 1:
self.pointer = [i]
break
def showFirstUnique(self) -> int:
return self.q[self.pointer] if self.pointer is not None else -1
def add(self, value: int) -> None:
self.q.append(value)
self.counts[value] += 1
if self.pointer is None and self.counts[value] == 1:
self.pointer = len(self.q) - 1
elif self.pointer is not None and self.q[self.pointer] == value:
while self.pointer < len(self.q) and self.counts[self.q[self.pointer]] > 1:
self.pointer += 1
if self.pointer >= len(self.q):
self.pointer = None