128. Longest Consecutive Sequence

30 篇文章 0 订阅
15 篇文章 0 订阅

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

Your algorithm should run in O(n) complexity.

Example:

Input: [100, 4, 200, 1, 3, 2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.

 

We will use HashMap. The key thing is to keep track of the sequence length and store that in the boundary points of the sequence. For example, as a result, for sequence {1, 2, 3, 4, 5}, map.get(1) and map.get(5) should both return 5.

Whenever a new element n is inserted into the map, do two things:

  1. See if n - 1 and n + 1 exist in the map, and if so, it means there is an existing sequence next to n. Variables left and right will be the length of those two sequences, while 0 means there is no sequence and n will be the boundary point later. Store (left + right + 1) as the associated value to key n into the map.
  2. Use left and right to locate the other end of the sequences to the left and right of n respectively, and replace the value with the new length.

Everything inside the for loop is O(1) so the total time is O(n). 

package l128;

import java.util.HashMap;
import java.util.Map;

class Solution {
    public int longestConsecutive(int[] nums) {
        Map<Integer, Integer>m=new HashMap<Integer, Integer>();
        for(int i:nums) {
        	if(m.containsKey(i)) continue;
        	int left=m.containsKey(i-1)?m.get(i-1):0;
        	int right=m.containsKey(i+1)?m.get(i+1):0;
        	
        	/*
        	 * Without it, the program may go wrong to handle duplicated num when there is only one neighbor number around it, 
        	 * or the consecutive area including it has changed longer on only one side, or both scenarios happen together.
        	 */
        	m.put(i, left+right+1);
        	
        	m.put(i-left, left+right+1);
        	m.put(i+right, left+right+1);
        }
        
        int res=0;
        for(int k:m.keySet()) res=Math.max(res, m.get(k));
        return res;
    }
    
    public static void main(String[] args) {
    	Solution s=new Solution();
    	System.out.println(s.longestConsecutive(new int[]{100, 4, 200, 1, 3, 2}));
	}
}

至于m.put(i, left+right+1); 这一行的作用:是让m记住有个i数值是存在的,因为后续可能还会用到

比如2把[-1,1] [3,4]连起来了,如果不把2记录下来,后面更长的[-10,1] [3,4]就合并不起来了

 

或者直接遍历所有以i开始的(因为最长的肯定是以数组中某个数开始的)

First turn the input into a set of numbers. That takes O(n) and then we can ask in O(1) whether we have a certain number.

Then go through the numbers. If the number x is the start of a streak (i.e., x-1 is not in the set), then test y = x+1, x+2, x+3, ... and stop at the first number y not in the set. The length of the streak is then simply y-x and we update our global best with that. Since we check each streak only once, this is overall O(n). This ran in 44 ms on the OJ, one of the fastest Python submissions.

def longestConsecutive(self, nums):
    nums = set(nums)
    best = 0
    for x in nums:
        if x - 1 not in nums:
            y = x + 1
            while y in nums:
                y += 1
            best = max(best, y - x)
    return best
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值