forked from Sanskar9218/Hactober-Contribution-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcs.java
44 lines (39 loc) · 945 Bytes
/
lcs.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
class Solution {
public int longestConsecutive(int[] nums) {
// HashSet<Integer> h = new HashSet<>();
PriorityQueue<Integer> p = new PriorityQueue<>();
HashSet<Integer> j = new HashSet<>();
for(int i=0;i<nums.length;i++)
{
if(!j.contains(nums[i]))
{
p.add(nums[i]);
j.add(nums[i]);
}
}
int count=1;
int max=1;
if(nums.length>0)
{
int a=p.poll();
while(p.peek()!=null)
{
if(p.peek()==a+1)
{
count++;
// a=p.poll();
if(count>max)
{
max=count;
}
}
else{
count=1;
}
a=p.poll();
}
return max;
}
return 0;
}
}