Tuesday, March 5, 2013

[LeetCode] Longest Consecutive Sequence

Thought:
We use a HashMap to store the interval's length.
leftbound -> len
rightboung -> len
when we insert a new value, we update the map.

Code:
public class Solution {
    public int longestConsecutive(int[] num) {
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        int max = 1;
        for (int i : num) {
            if (map.containsKey(i)) continue;
            map.put(i, 1);
            if (map.containsKey(i - 1)) max = Math.max(max, merge(map, i-1, i));
            if (map.containsKey(i + 1)) max = Math.max(max, merge(map, i, i+1));
        }
        return max;       
    }
   
    public int merge(HashMap<Integer, Integer> map, int left, int right) {
        int upper = right + map.get(right) - 1;
        int lower = left - map.get(left) + 1;
        int len = upper - lower + 1;
        map.put(upper, len);
        map.put(lower, len);
        return len;       
    }       
}

No comments:

Post a Comment