-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathLongestConsecutiveSequence.java
126 lines (98 loc) · 2.97 KB
/
LongestConsecutiveSequence.java
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
package Leetcode.Youtube;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class LongestConsecutiveSequence {
// O(n^2) time, O(n) space
public static int lengthOfLongestConsecutiveSequence(int[] arr, int N) {
if (N == 0)
return 0;
Map<Integer, Integer> map = new HashMap<>();
for (int n : arr) {
if (!map.containsKey(n)) {
map.put(n, 1);
} else {
map.put(n, map.get(n) + 1); // can skip this part
}
}
int max = 0;
for (int num : arr) {
int count = 1;
while (map.containsKey(num + 1)) {
count++;
num++;
}
max = Math.max(max, count);
}
return max;
}
// approach 2 using Sorting
// O(NlogN) time, O(N) space
public static int lengthOfLongestConsecutiveSequence2(int[] arr, int N) {
if (N == 0)
return 0;
int max = 1;
int[] arrclone = arr.clone(); // making copy of original array
Arrays.sort(arrclone); // sorting array
int ans = 1;
for (int i = 1; i < arrclone.length; i++) {
if (arrclone[i - 1] == arrclone[i] - 1) {
ans++;
max = Math.max(ans, max);
} else if (arrclone[i] == arrclone[i - 1]) { // for duplicate elements
continue;
} else {
ans = 1;
}
}
return max;
}
// approach 3, most optimal - using HashSet
// O(N) time | O(N) Space
public static int lengthOfLongestConsecutiveSequence3(int[] arr, int N) {
if (N == 0) {
return 0;
}
Set<Integer> myset = new HashSet<Integer>();
for (int num : arr) {
myset.add(num);
}
int maxlen = 1;
for (int n : arr) {
int currmax = 1;
if (myset.contains(n - 1)) // skip already searched sequence
continue;
else {
while (myset.contains(n + 1)) {
currmax++;
n++;
}
}
maxlen = Math.max(currmax, maxlen);
}
return maxlen;
}
public static void main(String[] args) {
int[] arr = { 100, 4, 200, 1, 3, 2 };
int[] arr2 = { 1, 5, 9, 13, 6, 12, 7, 11, 14, 15 };
int[] arr3 = { 9, 5, 4, 9, 10, 10, 6 };
int[] arr4 = { 0, 3, 7, 2, 5, 8, 4, 6, 0, 1 };
System.out.println("brute force solution...");
System.out.println(lengthOfLongestConsecutiveSequence(arr, 7));
System.out.println(lengthOfLongestConsecutiveSequence(arr2, 9));
System.out.println(lengthOfLongestConsecutiveSequence(arr3, 7));
System.out.println(lengthOfLongestConsecutiveSequence(arr4, 9));
System.out.println("\ntest using sorting technique...");
System.out.println(lengthOfLongestConsecutiveSequence2(arr, 7));
System.out.println(lengthOfLongestConsecutiveSequence2(arr2, 9));
System.out.println(lengthOfLongestConsecutiveSequence2(arr3, 7));
System.out.println(lengthOfLongestConsecutiveSequence2(arr4, 9));
System.out.println("\ntest using hashset...");
System.out.println(lengthOfLongestConsecutiveSequence3(arr, 7));
System.out.println(lengthOfLongestConsecutiveSequence3(arr2, 9));
System.out.println(lengthOfLongestConsecutiveSequence3(arr3, 7));
System.out.println(lengthOfLongestConsecutiveSequence3(arr4, 9));
}
}