-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path128_LongestConsecutiveSequence.java
60 lines (54 loc) · 1.74 KB
/
128_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
/*
* Given an unsorted array of integers, find the length
* of the longest consecutive elements sequence.
* For example,
* Given [100, 4, 200, 1, 3, 2],
* The longest consecutive elements sequence is
* [1, 2, 3, 4]. Return its length: 4.
* Your algorithm should run in O(n) complexity.
*/
public class Solution {
public int longestConsecutive(int[] num) {
Set<Integer> set = new HashSet<Integer>();
int res = 0;
for(int value : num) {
set.add(value);
}
for(int value : num) {
int low = value;
int high = value + 1;
while(set.contains(low - 1)) set.remove(low--);
while(set.contains(high)) set.remove(high++);
res = Math.max(res, high - low);
}
return res;
}
}
//Many other algorithm
https://github.com/mitcc/AlgoSolutions/blob/master/leetcode/LongestConsecutiveSequence.java
//Check it
public int longestConsecutive(int[] num) {
int res = 0;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int n : num) {
if (!map.containsKey(n)) {
int left = (map.containsKey(n - 1)) ? map.get(n - 1) : 0;
int right = (map.containsKey(n + 1)) ? map.get(n + 1) : 0;
// sum: length of the sequence n is in
int sum = left + right + 1;
map.put(n, sum);
// keep track of the max length
res = Math.max(res, sum);
// extend the length to the boundary(s)
// of the sequence
// will do nothing if n has no neighbors
map.put(n - left, sum);
map.put(n + right, sum);
}
else {
// duplicates
continue;
}
}
return res;
}