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.
Example 1:
**Input:** nums = [100,4,200,1,3,2]
**Output:** 4
**Explanation:** The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
class Solution {
public int longestConsecutive(int[] nums) {
// edge cases
if (nums.length < 2) return nums.length;
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
for (int i : nums) {
pq.offer(i);
}
int max = 1;
int len = 1;
int prev = pq.poll();
while (!pq.isEmpty()) {
int n = pq.poll();
if (n == prev + 1) {
len++;
max = Math.max(max, len);
} else if (n > prev + 1) {
len = 1;
}
prev = n;
}
return max;
}
}
// priority queue
ac2: set
class Solution {
public int longestConsecutive(int[] nums) {
// edge cases
if (nums == null || nums.length == 0) return 0;
Set<Integer> set = new HashSet<>();
for (int i : nums) set.add(i);
int lgst = 0;
for (int n : set) {
if (set.contains(n - 1)) continue; // 100, 4, 200, 1, 3, 2. if meet 3, skip, cause we will start from 1
int cnt = 0;
while (set.contains(n)) {
cnt++;
n++;
}
lgst = Math.max(lgst, cnt);
}
return lgst;
}
}
/*
sort, nlogn
set, O(n)
map, O(n)
*/
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;
}