-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestConsecutiveSequence.java
More file actions
531 lines (368 loc) · 12.2 KB
/
LongestConsecutiveSequence.java
File metadata and controls
531 lines (368 loc) · 12.2 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
package Algorithms.Hashing;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.
You must write an algorithm that runs in O(n) time.
*
*
* @author Srinvas Vadige, srinivas.vadige@gmail.com
* @since 24 Sept 2024
* @link 128. Longest Consecutive Sequence <a href="https://leetcode.com/problems/longest-consecutive-sequence/">Leetcode link</a>
* @topics Array, Hash Table, Sorting, Union Find
*/
public class LongestConsecutiveSequence {
public static void main(String[] args) {
int[] nums = {100,4,200,1,3,2};
System.out.println( "longestConsecutiveUsingHashSet: " + longestConsecutiveUsingHashSet(nums));
System.out.println("longestConsecutiveUsingSort: " + longestConsecutiveUsingSort(nums));
System.out.println("longestConsecutiveUsingUnionFind: " + longestConsecutiveUsingUnionFind(nums));
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(n)
*
* The reason why it's O(n) is
* 1. Insert all nums in set (we skip the duplicates as well)
* 2. Iterate through set
* 3. In best case when no sequence --> then O(n)
* 4. In worst case when few of then are consecutive --> then O(n) + O(n) + O(n)... few
* 5. Which is less than O(n^2)
*/
public static int longestConsecutiveUsingHashSet(int[] nums) {
Set<Integer> seen = new HashSet<>();
for(int num: nums) {
seen.add(num);
}
int max = 0;
for (int num: seen) {
if (seen.contains(num-1)) continue; // i.e not head
int len = 0;
while(seen.contains(num)) {
len++;
num++;
}
max = Math.max(max, len);
}
return max;
}
public static int longestConsecutiveUsingHashSet2(int[] nums) {
Set<Integer> set = new HashSet<>();
for(int num: nums) {
set.add(num);
}
int max = 0;
for (int num: set) {
if (!set.contains(num-1)) { // i.e not head
int len = 0;
while(set.contains(num+len)) len++;
max = Math.max(max, len);
}
}
return max;
}
public static int longestConsecutiveUsingHashSet3(int[] nums) {
if (nums == null || nums.length == 0) return 0;
Set<Integer> numSet = new HashSet<>();
for (int num : nums) numSet.add(num);
int longestStreak = 0;
// Iterate through the set instead of arr and find the "start" of each sequence
for (int num : numSet) {
// Only start counting if "num" is the start of a sequence, cause we'll trav it later
if (!numSet.contains(num - 1)) { // or if(numSet.contains(num-1)) continue;
int currentStreak = 1;
while (numSet.contains(num + 1)) { // sequence
num++;
currentStreak++;
}
longestStreak = Math.max(longestStreak, currentStreak);
}
}
return longestStreak;
}
public static int longestConsecutiveUsingDoubleLinkedListAndHashMap(int[] nums) {
class Node {
final int val;
Node left;
Node right;
Node(int val) {this.val = val;}
}
Map<Integer, Node> map = new HashMap<>();
// Prepare DoubleLinkedLists
for(int num: nums) {
if (map.containsKey(num)) {
continue;
}
Node curr = new Node(num);
map.put(num, curr);
Node left = map.get(num-1);
Node right = map.get(num+1);
if (left != null) {
curr.left = left;
left.right = curr;
}
if (right != null) {
curr.right = right;
right.left = curr;
}
}
// trav only the head nodes
int maxLen = 0;
for (Node node : map.values()) {
if (node.left != null) continue; // i.e not head
int len = 0;
for (Node cur = node; cur != null; cur = cur.right) len++;
maxLen = Math.max(maxLen, len);
}
return maxLen;
}
public static int longestConsecutiveUsingSort(int[] nums) {
int[] sortedNonDupNums = Arrays.stream(nums).distinct().sorted().toArray(); // or re-assign it to given "nums" param
int maxLen = 0;
int tempLen = 0;
if (nums.length > 0) {
tempLen = 1;
maxLen = 1;
}
for(int i=1; i<sortedNonDupNums.length; i++) {
if(sortedNonDupNums[i-1]+1 == sortedNonDupNums[i]) {
tempLen++;
maxLen = Math.max(maxLen, tempLen);
} else {
tempLen = 1;
}
}
return maxLen;
}
public static int longestConsecutiveUsingSort2(int[] nums) {
Arrays.sort(nums);
int maxLen = 0;
int tempLen = 0;
if (nums.length > 0) {
tempLen = 1;
maxLen = 1;
}
for(int i=1; i<nums.length; i++) {
if(nums[i-1]+1 == nums[i]) {
tempLen++;
maxLen = Math.max(maxLen, tempLen);
} else if(nums[i-1] == nums[i]) { // skip dups
continue;
} else {
tempLen = 1;
}
}
return maxLen;
}
public static int longestConsecutiveUsingSort3(int[] nums) {
if(nums.length == 0) return 0;
Arrays.sort(nums);
int tempMax = 1; // at least one item in nums array
int max = 1;
int prev = nums[0];
for (int i = 1; i < nums.length; i++){
if (prev == nums[i]) continue; // dups
else if(prev+1 == nums[i]){ // sequence
tempMax++;
max = Math.max(max, tempMax);
}
else tempMax = 1; // sequence broken
// update prev pointer
prev = nums[i];
}
return max;
}
/**
* It's same as {@link #longestConsecutiveUsingHashSet(int[])}
*/
public static int longestConsecutiveUsingUnionFind(int[] nums) {
if (nums == null || nums.length == 0) return 0;
Set<Integer> numSet = new HashSet<>();
for (int num : nums) numSet.add(num);
UF uf = new UF(nums);
for (int num : numSet) {
if (numSet.contains(num+1)) {
uf.union(num, num+1);
}
}
Map<Integer, Integer> rootCount = new HashMap<>();
for (int num: numSet) rootCount.merge(uf.find(num), 1, Integer::sum);
return Collections.max(rootCount.values());
}
static class UF {
Map<Integer, Integer> map = new HashMap<>(); // num -> parent
public UF(int[] nums) {
for (int num : nums) map.put(num, num);
}
public int find(int num) {
while (map.get(num) != num) num = map.get(num);
return num;
}
public void union(int num1, int num2) {
int parent1 = find(num1);
int parent2 = find(num2);
if (parent1 != parent2) map.put(parent2, parent1);
}
}
public static int longestConsecutiveUsingUnionFind2(int[] nums) {
if (nums == null || nums.length == 0) return 0;
Set<Integer> numSet = new HashSet<>();
for (int num : nums) numSet.add(num);
UnionFind uf = new UnionFind(numSet.size());
Map<Integer, Integer> numToIndex = new HashMap<>();
int index = 0;
for (int num : numSet) numToIndex.put(num, index++);
for (int num : numSet) {
if (numSet.contains(num - 1)) {
uf.union(numToIndex.get(num), numToIndex.get(num - 1));
}
}
int max = 0;
for (int num : numSet) {
max = Math.max(max, uf.rank[uf.find(numToIndex.get(num))]);
}
return max;
}
static class UnionFind {
int[] parent;
int[] rank;
public UnionFind(int n) {
parent = new int[n];
rank = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
rank[i] = 1;
}
}
public void union(int i, int j) {
int pi = find(i);
int pj = find(j);
if (pi == pj) return;
if (rank[pi] < rank[pj]) {
parent[pi] = pj;
rank[pj] += rank[pi];
} else {
parent[pj] = pi;
rank[pi] += rank[pj];
}
}
public int find(int i) {
if (parent[i] == i) return i;
return parent[i] = find(parent[i]);
}
}
/**
PATTERNS:
---------
1) Instead of sort, we need to solve this in O(n) time complexity
2) HashSet and now for(nums) loop to check lWidth and rWidth of i.
3) Dups are not part of sequence
*/
public static int longestConsecutiveHashSetMyApproachOld(int[] nums) {
int n = nums.length;
Set<Integer> set = new HashSet<>();
Set<Integer> visited = new HashSet<>();
for(int x: nums) set.add(x);
int max = 0;
for (int num : nums) {
int x = num;
if (visited.contains(x)) continue;
int lc = 0;
int rc = 0;
visited.add(x);
// left
while (set.contains(x - 1)) {
x = x - 1;
lc++;
visited.add(x);
}
// right
x = num;
while (set.contains(x + 1)) {
x = x + 1;
rc++;
visited.add(x);
}
max = Math.max(max, lc + rc + 1);
}
return max;
}
public int longestConsecutiveNotWorkingMemoryLimitExceeded(int[] nums) {
boolean[] negatives = new boolean[(int)1e9+1];
boolean[] positives = new boolean[(int)1e9+1];
for(int num: nums) {
if(num < 0) {
negatives[Math.abs(num)] = true;
} else {
positives[num] = true;
}
}
int maxLen = 0;
int tempLen = 0;
for(int i=1; i<=1e9; i++) {
if(negatives[i]) {
tempLen++;
maxLen = Math.max(maxLen, tempLen);
} else {
tempLen = 0;
}
}
tempLen = 0;
// last -ve count i.e -1 to till valid -ve consecutive number
for(int i=1; i<=1e9; i++) {
if(negatives[i]) {
tempLen++;
maxLen = Math.max(maxLen, tempLen);
} else {
break;
}
}
for(int i=0; i<=1e9; i++) {
if(positives[i]) {
tempLen++;
maxLen = Math.max(maxLen, tempLen);
} else {
tempLen = 0;
}
}
return maxLen;
}
public static int longestConsecutiveUsingHashSetTLE(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums) {
set.add(num);
}
int longest = 0;
for (int num : nums) {
if (!set.contains(num - 1)) {
int length = 1;
while (set.contains(num + length)) length++;
longest = Math.max(longest, length);
}
}
return longest;
}
/**
* TLE
*/
public static int longestConsecutiveTLE(int[] nums) {
Set<Integer> numSet = new HashSet<>();
for (int num : nums) {
numSet.add(num);
}
int longest = 0;
for (int num : nums) {
if (!numSet.contains(num - 1)) {
int length = 1;
while (numSet.contains(num + length))
length++;
longest = Math.max(longest, length);
}
}
return longest;
}
}