Skip to content

Commit 8961855

Browse files
committed
Sync LeetCode submission Runtime - 543 ms (82.32%), Memory - 61.3 MB (49.31%)
1 parent 1323ee1 commit 8961855

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

1366-first-unique-number/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<p>You have a queue of integers, you need to retrieve the first unique integer in the queue.</p>
2+
3+
<p>Implement the <code>FirstUnique</code>&nbsp;class:</p>
4+
5+
<ul>
6+
<li><code>FirstUnique(int[] nums)</code> Initializes the object with the numbers in the queue.</li>
7+
<li><code>int showFirstUnique()</code>&nbsp;returns the value of <strong>the&nbsp;first unique</strong> integer of the queue, and returns <strong>-1</strong> if there is no such integer.</li>
8+
<li><code>void add(int value)</code>&nbsp;insert value&nbsp;to&nbsp;the queue.</li>
9+
</ul>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<pre>
15+
<b>Input: </b>
16+
[&quot;FirstUnique&quot;,&quot;showFirstUnique&quot;,&quot;add&quot;,&quot;showFirstUnique&quot;,&quot;add&quot;,&quot;showFirstUnique&quot;,&quot;add&quot;,&quot;showFirstUnique&quot;]
17+
[[[2,3,5]],[],[5],[],[2],[],[3],[]]
18+
<b>Output: </b>
19+
[null,2,null,2,null,3,null,-1]
20+
<b>Explanation: </b>
21+
FirstUnique firstUnique = new FirstUnique([2,3,5]);
22+
firstUnique.showFirstUnique(); // return 2
23+
firstUnique.add(5); // the queue is now [2,3,5,5]
24+
firstUnique.showFirstUnique(); // return 2
25+
firstUnique.add(2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // the queue is now [2,3,5,5,2]
26+
firstUnique.showFirstUnique(); // return 3
27+
firstUnique.add(3);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // the queue is now [2,3,5,5,2,3]
28+
firstUnique.showFirstUnique(); // return -1
29+
</pre>
30+
31+
<p><strong class="example">Example 2:</strong></p>
32+
33+
<pre>
34+
<b>Input: </b>
35+
[&quot;FirstUnique&quot;,&quot;showFirstUnique&quot;,&quot;add&quot;,&quot;add&quot;,&quot;add&quot;,&quot;add&quot;,&quot;add&quot;,&quot;showFirstUnique&quot;]
36+
[[[7,7,7,7,7,7]],[],[7],[3],[3],[7],[17],[]]
37+
<b>Output: </b>
38+
[null,-1,null,null,null,null,null,17]
39+
<b>Explanation: </b>
40+
FirstUnique firstUnique = new FirstUnique([7,7,7,7,7,7]);
41+
firstUnique.showFirstUnique(); // return -1
42+
firstUnique.add(7); // the queue is now [7,7,7,7,7,7,7]
43+
firstUnique.add(3);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // the queue is now [7,7,7,7,7,7,7,3]
44+
firstUnique.add(3);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // the queue is now [7,7,7,7,7,7,7,3,3]
45+
firstUnique.add(7);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // the queue is now [7,7,7,7,7,7,7,3,3,7]
46+
firstUnique.add(17);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// the queue is now [7,7,7,7,7,7,7,3,3,7,17]
47+
firstUnique.showFirstUnique(); // return 17
48+
</pre>
49+
50+
<p><strong class="example">Example 3:</strong></p>
51+
52+
<pre>
53+
<b>Input: </b>
54+
[&quot;FirstUnique&quot;,&quot;showFirstUnique&quot;,&quot;add&quot;,&quot;showFirstUnique&quot;]
55+
[[[809]],[],[809],[]]
56+
<b>Output: </b>
57+
[null,809,null,-1]
58+
<b>Explanation: </b>
59+
FirstUnique firstUnique = new FirstUnique([809]);
60+
firstUnique.showFirstUnique(); // return 809
61+
firstUnique.add(809); // the queue is now [809,809]
62+
firstUnique.showFirstUnique(); // return -1
63+
</pre>
64+
65+
<p>&nbsp;</p>
66+
<p><strong>Constraints:</strong></p>
67+
68+
<ul>
69+
<li><code>1 &lt;= nums.length &lt;= 10^5</code></li>
70+
<li><code>1 &lt;= nums[i] &lt;= 10^8</code></li>
71+
<li><code>1 &lt;= value &lt;= 10^8</code></li>
72+
<li>At most <code>50000</code>&nbsp;calls will be made to <code>showFirstUnique</code>&nbsp;and <code>add</code>.</li>
73+
</ul>

1366-first-unique-number/solution.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Approach 2: Queue and HashMap of Unique-Status
2+
3+
from collections import deque
4+
5+
class FirstUnique:
6+
7+
def __init__(self, nums: List[int]):
8+
self.queue = deque(nums)
9+
self.is_unique = {}
10+
for num in nums:
11+
self.add(num)
12+
13+
14+
def showFirstUnique(self) -> int:
15+
while self.queue and not self.is_unique[self.queue[0]]:
16+
self.queue.popleft()
17+
18+
if self.queue:
19+
return self.queue[0]
20+
return -1
21+
22+
23+
def add(self, value: int) -> None:
24+
if value not in self.is_unique:
25+
self.is_unique[value] = True
26+
self.queue.append(value)
27+
else:
28+
self.is_unique[value] = False
29+
30+
31+
32+
# Your FirstUnique object will be instantiated and called as such:
33+
# obj = FirstUnique(nums)
34+
# param_1 = obj.showFirstUnique()
35+
# obj.add(value)

0 commit comments

Comments
 (0)