[LeetCode] Longest Consecutive Sequence(DP)

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.

方法1:显然运行不是O(n)的时间复杂度,因此Time Limit Exceeded!

 class Solution {
public:
    int longestConsecutive(vector<int> &num) {
        int len = num.size();
        if(len<=1)
            return len;
        int max = 1;
        map<int,int> m;//key is the number in num,value is the longest consecutive number from the  current key to  the bigger
        for(int i=0;i<len;i++){

            if(m.empty())
                m[num[i]] = 1;
            else if(m.count(num[i]+1)!= 0)
            {
                int number = num[i];
                m[num[i]] = m[num[i]+1]+1;

                while(m.count(number-1)!= 0)
                {
                   m[number-1] = m[number]+1;
                   number--;
                }
                max = max>m[number] ? max :m[number];
            }
            else{
                int number = num[i];
                m[num[i]] = 1;
                while(m.count(number-1)!= 0)
                {
                   m[number-1] = m[number]+1;
                   number--;
                }
                max = max>m[number] ? max :m[number];
            }

        }//end for
        return max;
    }
};

方法2:其实和方法1一样的思想,只是用了map<int,vector<int>::iterator>来存储每个元素如果连续的话的上界或者下界,大大简化了
           方法1中的2个while循环,这就是方法2改进的地方了。

class Solution {
public:
    int longestConsecutive(vector<int> &num) {
        map<int,int> vTable;//v(x) = the max length of consecutive sequence starting from x
        map<int,vector<int>::iterator> aTable;
        for (vector<int>::iterator i = num.begin(); i!=num.end(); i++) {
            if(vTable.count(*i)) continue;      // Ignore same number
            vTable[*i]=1;
            aTable[*i]=i;                       // Initialization of new input
            if(vTable.count(*i+1)) {            // If i+1 exists
                vTable[*i] += vTable[*i+1];     // Update v(x)
                aTable[*i] = aTable[*i+1];      // Update a(x)
            }
            if(vTable.count(*i-1)) {            // If i-1 exists, same idea
                vTable[*aTable[*i-1]] += vTable[*i];
                aTable[*aTable[*i]] = aTable[*i-1];
                aTable[*aTable[*i-1]] = (vTable.count(*i+1)) ? aTable[*i] : i;
            }else aTable[*aTable[*i]] = i;
        }
        int max=0; // Find max in vTable
        map<int,int>::iterator iter ;
        for (iter = vTable.begin();iter!= vTable.end();iter++)
            if ((*iter).second>max)
                max = (*iter).second;
        return max;
    }
};

[LeetCode] Longest Consecutive Sequence(DP),布布扣,bubuko.com

时间: 2024-12-14 18:52:34

[LeetCode] Longest Consecutive Sequence(DP)的相关文章

128. Longest Consecutive Sequence(js)

128. Longest Consecutive Sequence 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 longe

leetcode 之Longest Consecutive Sequence(六)

这题要仔细体会下哈希表的用法,要注意的是数组本身是无序的,因此需要向左右进行扩张. 另外这个思路可以进行聚类,把连续的标记为一类. int longestConsecutive(const vector<int> &num) { unordered_map<int, bool> used; for (auto i : num)used[i] = false; int longest = 0; for (auto i : num) { if (used[i])continue

HDU 4908 (杭电 BC #3 1002题)BestCoder Sequence(DP)

题目地址:HDU 4908 这个题是从m开始,分别往前DP和往后DP,如果比m大,就比前面+1,反之-1.这样的话,为0的点就可以与m这个数匹配成一个子串,然后左边和右边的相反数的也可以互相匹配成一个子串,然后互相的乘积最后再加上就行了.因为加入最终两边的互相匹配了,那就说明左右两边一定是偶数个,加上m就一定是奇数个,这奇数个的问题就不用担心了. 代码如下: #include <iostream> #include <stdio.h> #include <string.h&g

LeetCode——Longest Consecutive Sequence

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 i

[leetcode]Longest Consecutive Sequence @ Python

原题地址:https://oj.leetcode.com/problems/longest-consecutive-sequence/ 题意: 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 seque

[LeetCode] Palindrome Partitioning II (DP)

Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa"

LeetCode: Longest Consecutive Sequence 解题报告

Longest Consecutive Sequence 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.

【leetcode】Longest Consecutive Sequence(hard)☆

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

[Leetcode] Longest consecutive sequence 最长连续序列

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