[LintCode] Longest Consecutive Sequence

本文介绍了一种算法,用于找出未排序整数数组中最长的连续元素序列长度,并提供了三种解决方案,包括使用排序、最小优先队列及哈希集合的方法。

Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

Clarification

Your algorithm should run in O(n) complexity.

Example

Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.

 

Solution 1.  O(n * log n) runtime using sorting 

Algorithm:

1. sort the array;

2. iterate through the sorted array and process each element in the following 3 possible cases.

  a. num[i] - num[i - 1] == 1, currMaxLen++;

  b. num[i] - num[i - 1] != 1 && num[i] != num[i - 1], the current sequence discontinues, reset currMaxLen to 1;

  c. num[i] == num[i - 1], the current sequence should continue, do nothing. 

 

 1 public class Solution {
 2     public int longestConsecutive(int[] num) {
 3         if(num == null || num.length == 0){
 4             return 0;
 5         }
 6         Arrays.sort(num);
 7         int maxLen = 1, currMaxLen = 1;
 8         for(int i = 1; i < num.length; i++){
 9             if(num[i] - num[i - 1] == 1){
10                 currMaxLen++;
11             }
12             else if(num[i] != num[i - 1]){
13                 currMaxLen = 1;
14             }
15             maxLen = Math.max(maxLen, currMaxLen);
16         }
17         return maxLen;
18     }
19 }

 

Solution 2. O(n * log n) runtime, O(n) space using min priority queue.

Similiarly with solution 1, we can use a min pq to replace the effect of sorting.

 1 public class Solution {
 2     public int longestConsecutive(int[] num) {
 3         if(num == null || num.length == 0){
 4             return 0;
 5         }
 6         PriorityQueue<Integer> minPq = new PriorityQueue<Integer>(num.length);
 7         for(int i = 0; i < num.length; i++){
 8             minPq.add(num[i]);
 9         }
10         int prevMin = Integer.MAX_VALUE, currMin = Integer.MIN_VALUE;
11         int currMaxLen = 1, maxLen = Integer.MIN_VALUE;
12         while(minPq.size() != 0){
13             currMin = minPq.poll();
14             if(currMin - prevMin == 1){
15                 currMaxLen++;
16             }
17             else if(currMin != prevMin){
18                 currMaxLen = 1;
19             }
20             prevMin = currMin;
21             maxLen = Math.max(maxLen, currMaxLen);
22         }
23         return maxLen;
24     }
25 }

 

Solution 3.  O(n) runtime, O(n) space, Using HashSet

Algorithm:  Store all elements into a hash set, thus only unique elements are stored. 

Iterate through all elements in num. For each num[i], go down and up to find the longest

consecutive sequence that has num[i] in it.  In each iteration, delete all the elements

that appear in the current consecutive sequence. Doing this makes this algorithm more

efficient as it will not redudantly consider the same sequence more than once. 

 

 1 public class Solution {
 2     public int longestConsecutive(int[] num) {
 3         if(num == null || num.length == 0){
 4             return 0;
 5         } 
 6         HashSet<Integer> set = new HashSet<Integer>();
 7         for(int i = 0; i < num.length; i++){
 8             set.add(num[i]);
 9         }
10         int longest = 0;
11         for(int i = 0; i < num.length; i++){
12             int down = num[i] - 1;
13             while(set.contains(down)){
14                 set.remove(down);
15                 down--;
16             }
17             int up = num[i] + 1;
18             while(set.contains(up)){
19                 set.remove(up);
20                 up++;
21             }
22             set.remove(num[i]);
23             longest = Math.max(longest, up - down - 1);
24         }
25         return longest;
26     }
27 }

 

 

 

Related Problems 

Binary Tree Longest Consecutive Sequence 

Binary Tree Longest Consecutive Sequence III

Longest Increasing Subsequence 

 

转载于:https://www.cnblogs.com/lz87/p/7203661.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值