My dp solution with 12ms c++


  • -31
    J

    `class Solution {
    public:
    int longestConsecutive(vector<int>& nums) {
    if(!nums.size()) return 0;
    sort(nums.begin(),nums.end());
    int max_length = 1;
    int front_length = 1;
    for(int i = 1;i<nums.size();i++){
    if(nums[i-1]+1==nums[i]){
    front_length++;
    max_length = max(max_length,front_length);
    }
    else
    front_length = nums[i-1]==nums[i]? front_length:1;
    }

        return max_length;
        
    }
    

    };`


  • 0
    F

    I see “sort(nums.begin(),nums.end()); ” rofl
    If you have sorted the array, what's the point of DP, just go through the array and count, lol


  • 0
    J

    if it's a dp solution, then it's not an O(n) solution


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.