-
Notifications
You must be signed in to change notification settings - Fork 1
/
128. Longest Consecutive Sequence.java
85 lines (63 loc) · 2.04 KB
/
128. Longest Consecutive Sequence.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
//BRUTE FORCE APPROACH - BEATS 80% in runtime - O(nlogn)
class Solution {
public int longestConsecutive(int[] nums) {
HashSet set = new HashSet<Integer>();
for(int i:nums)
set.add(i);
if(nums.length==0)
{
return 0;
}
ArrayList<Integer> arr = new ArrayList<>(set);
Collections.sort(arr);
int cur=1,max = 1;
for(int i=0;i<arr.size()-1;i++)
{
if(arr.get(i)+1==arr.get(i+1))
{
cur = cur +1;
}
else
{
cur = 1;
}
if(cur>max)
{
max = cur;
}
}
return max;
}
}
//OPTIMAL - O(n)
//intuition - https://www.youtube.com/watch?v=oO5uLE7EUlM&list=PLgUwDviBIf0oF6QL8m22w1hIDC1vJ_BHz&index=29
// add all numbers to hashset to avoid all duplicates, now we will check for streak.
//current streak will be your current pointers streak and max streak will be maximum streak yet,
// loop over your set, and on a while loop check if next bigger number exists and increase your current streak
// if not then you can try finding another starting point and make your current streak 1.
class Solution {
public int longestConsecutive(int[] nums) {
if(nums.length==0)
return 0;
HashSet<Integer> set = new HashSet<Integer>();
for(int i:nums)
set.add(i);
int maxstreak =0;
int currentstreak =1;
for(int i: set)
{
if(! set.contains(i-1))
{
int cur =i;
currentstreak = 1;
while(set.contains(cur+1))
{
cur=cur+1;
currentstreak = currentstreak+1;
}
maxstreak = Math.max(currentstreak, maxstreak);
}
}
return maxstreak;
}
}