I like to read this solution as a small machine: keep the useful information, throw away the noise. For First Unique Number, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
The notes already sitting in the source point us in the right direction:
- TLE
- 15 / 17 test cases passed.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are showFirstUnique, add.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//TLE
02//15 / 17 test cases passed.
03class FirstUnique {
04public:
05 map<int, int> counter;
06 vector<int> uniqs;
07
08 FirstUnique(vector<int>& nums) {
09 for(int num : nums){
10 counter[num] += 1;
11 if(counter[num] == 1){
12 uniqs.push_back(num);
13 }else{
14 vector<int>::iterator position = find(uniqs.begin(), uniqs.end(), num);
15 if (position != uniqs.end())
16 uniqs.erase(position);
17 }
18 }
19 }
20
21 int showFirstUnique() {
22 return uniqs.size() > 0 ? uniqs[0] : -1;
23 }
24
25 void add(int value) {
26 if(counter.find(value) == counter.end()){
27 counter[value] = 1;
28 uniqs.push_back(value);
29 }else{
30 counter[value]++;
31 vector<int>::iterator position = find(uniqs.begin(), uniqs.end(), value);
32 if (position != uniqs.end())
33 uniqs.erase(position);
34 }
35 }
36};
37
38/**
39 * Your FirstUnique object will be instantiated and called as such:
40 * FirstUnique* obj = new FirstUnique(nums);
41 * int param_1 = obj->showFirstUnique();
42 * obj->add(value);
43 */
44
45//double linked list
46//Runtime: 1204 ms
47//Memory Usage: 91.3 MB
48class DLinkedNode{
49public:
50 DLinkedNode *prev, *next;
51 int value;
52 DLinkedNode(int v) : value(v){
53 prev = nullptr;
54 next = nullptr;
55 };
56};
57
58class FirstUnique {
59public:
60 map<int, int> counter;
61 DLinkedNode *head, *tail;
62 vector<int> uniqs;
63
64 FirstUnique(vector<int>& nums) {
65 head = new DLinkedNode(-1);
66 tail = new DLinkedNode(-1);
67 head->prev = nullptr;
68 head->next = tail;
69 tail->prev = head;
70 tail->next = nullptr;
71 for(int num : nums){
72 counter[num] += 1;
73 if(counter[num] == 1){
74 DLinkedNode* node = new DLinkedNode(num);
75 node->prev = tail->prev;
76 node->next = tail;
77
78 tail->prev->next = node;
79 tail->prev = node;
80 }else{
81 DLinkedNode *cur = head;
82 while(cur != nullptr){
83 if(cur->value == num){
84 //remove this node
85 cur->prev->next = cur->next;
86 cur->next->prev = cur->prev;
87 delete cur;
88 cur = nullptr;
89 break;
90 }
91 cur = cur->next;
92 }
93 }
94 }
95 }
96
97 int showFirstUnique() {
98 return head->next != tail ? head->next->value : -1;
99 }
100
101 void add(int value) {
102 if(counter.find(value) == counter.end()){
103 counter[value] = 1;
104
105 DLinkedNode* node = new DLinkedNode(value);
106 node->prev = tail->prev;
107 node->next = tail;
108
109 tail->prev->next = node;
110 tail->prev = node;
111 }else{
112 if(counter[value] == 1){
113 DLinkedNode *cur = head;
114 while(cur != nullptr){
115 if(cur->value == value){
116 //remove this node
117 cur->prev->next = cur->next;
118 cur->next->prev = cur->prev;
119 delete cur;
120 cur = nullptr;
121 break;
122 }
123 cur = cur->next;
124 }
125 }
126 counter[value]++;
127 }
128 }
129};
130
131/**
132 * Your FirstUnique object will be instantiated and called as such:
133 * FirstUnique* obj = new FirstUnique(nums);
134 * int param_1 = obj->showFirstUnique();
135 * obj->add(value);
136 */
Cost