-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy path128. Longest Consecutive Sequence.cpp
249 lines (205 loc) · 6.73 KB
/
128. Longest Consecutive Sequence.cpp
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//Runtime Error
//4 / 68 test cases passed.
//to do: merge interval
class DSU{
public:
vector<int> parent;
DSU(int n){
parent = vector<int>(n);
iota(parent.begin(), parent.end(), 0);
}
int find(int x){
if(parent[x] != x){
//the recursion stops when x == parent[x]
parent[x] = find(parent[x]);
}
return parent[x];
};
int unite(int x, int y){
//merge y into x
parent[y] = find(parent[x]);
return parent[y];
}
};
class Solution {
public:
unordered_map<int, int> group_head;
unordered_map<int, int> group_size;
int longestConsecutive(vector<int>& nums) {
set<int> snums(nums.begin(), nums.end());
nums = vector<int>(snums.begin(), snums.end());
DSU dsu(nums.size());
int ans = 0;
for(int num : nums){
// cout << "num: " << num << endl;
int h;
if(group_head.find(num-1) != group_head.end()){
h = dsu.unite(num-1, num);
group_head[num] = h;
++group_size[h];
}else if(group_head.find(num+1) != group_head.end()){
h = dsu.unite(num, num+1);
group_head[num] = h;
++group_size[h];
}else{
group_head[num] = num;
h = num;
++group_size[h];
}
// cout << "head: " << h << endl;
ans = max(ans, group_size[h]);
}
return ans;
}
};
//Union Find
//https://leetcode.com/problems/longest-consecutive-sequence/discuss/41062/My-Java-Solution-using-UnionFound
//Runtime: 28 ms, faster than 38.15% of C++ online submissions for Longest Consecutive Sequence.
//Memory Usage: 12.6 MB, less than 5.01% of C++ online submissions for Longest Consecutive Sequence.
class DSU{
public:
vector<int> parent;
DSU(int n){
parent = vector<int>(n);
iota(parent.begin(), parent.end(), 0);
}
int find(int x){
if(x != parent[x]){
parent[x] = find(parent[x]);
}
return parent[x];
}
void unite(int x, int y){
//merge y into x
parent[y] = find(parent[x]);
}
int maxUnion(){
unordered_map<int, int> groupSizes;
int maxSize = 0;
for(int i = 0; i < parent.size(); ++i){
++groupSizes[find(i)];
maxSize = max(maxSize, groupSizes[find(i)]);
}
return maxSize;
}
};
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
int n = nums.size();
DSU dsu(n);
unordered_map<int, int> val2idx;
for(int i = 0; i < n; ++i){
//ignore duplicate element
if(val2idx.find(nums[i]) != val2idx.end()) continue;
val2idx[nums[i]] = i;
if(val2idx.find(nums[i]-1) != val2idx.end()){
dsu.unite(val2idx[nums[i]-1], i);
}
if(val2idx.find(nums[i]+1) != val2idx.end()){
dsu.unite(i, val2idx[nums[i]+1]);
}
}
return dsu.maxUnion();
}
};
//Brute force
//TLE
//66 / 68 test cases passed.
//time: O(N^3), space: O(1)
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
int maxStreak = 0;
for(int num : nums){ //O(n)
int cur = num;
while(find(nums.begin(), nums.end(), cur+1) != nums.end()){
//while loop: O(n)
//find: O(n)
++cur;
}
//cur in nums, cur+1 not in nums
maxStreak = max(maxStreak, cur-num+1);
// cout << num << ", " << cur << ", " << maxStreak << endl;
}
return maxStreak;
}
};
//Sorting
//Runtime: 16 ms, faster than 93.81% of C++ online submissions for Longest Consecutive Sequence.
//Memory Usage: 11 MB, less than 59.85% of C++ online submissions for Longest Consecutive Sequence.
//time: O(NlogN), space: O(1) or O(N)
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
int maxStreak = 0;
unordered_set<int> snums(nums.begin(), nums.end());
nums = vector<int>(snums.begin(), snums.end());
sort(nums.begin(), nums.end());
int n = nums.size();
for(int i = 0; i < n; ){
int j = i;
while(j+1 < n && nums[j]+1 == nums[j+1]){
++j;
}
//nums[i...j] is a group
maxStreak = max(maxStreak, j-i+1);
i = j+1;
}
return maxStreak;
}
};
//Sorting, with duplicate
//Runtime: 16 ms, faster than 93.81% of C++ online submissions for Longest Consecutive Sequence.
//Memory Usage: 9.8 MB, less than 98.38% of C++ online submissions for Longest Consecutive Sequence.
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
if(nums.empty()) return 0;
int curStreak = 1;
int maxStreak = 1;
sort(nums.begin(), nums.end());
int n = nums.size();
for(int i = 1; i < n; ++i){
if(nums[i] == nums[i-1]) continue;
if(nums[i] == nums[i-1]+1){
//extend old group
++curStreak;
}else{
//start a new group
maxStreak = max(maxStreak, curStreak);
curStreak = 1;
}
}
//update with last group
maxStreak = max(maxStreak, curStreak);
return maxStreak;
}
};
//Approach 3: HashSet and Intelligent Sequence Building
//Runtime: 16 ms, faster than 93.81% of C++ online submissions for Longest Consecutive Sequence.
//Memory Usage: 11 MB, less than 49.83% of C++ online submissions for Longest Consecutive Sequence.
//time: O(N), space: O(N)
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_set<int> snums(nums.begin(), nums.end());
int maxStreak = 0;
for(const int num : snums){
if(snums.find(num-1) == snums.end()){
//that means num is the head of its group
int cur = num;
/*
this can only run for n iterations,
because the outside "if" only let the head of a group in
*/
while(snums.find(cur+1) != snums.end()){
++cur;
}
//[num...cur] is a group
maxStreak = max(maxStreak, cur-num+1);
}
}
return maxStreak;
}
};