Java 95% better solution

  • 0
    class Solution {
        public int longestConsecutive(int[] nums) {
            if(nums.length==0) return 0;
            int so_far=0,so_far_max=0,max=0;
            for(int i=1;i<nums.length;i++){
                if(nums[i]==nums[i-1]) continue;
                so_far = (nums[i]-nums[i-1])==1?so_far+1:0;
                so_far_max = so_far_max>so_far?so_far_max:so_far;
                max = max>so_far_max?max:so_far_max;
            return max+1;

  • 0

    The problem asks you to do it in O(n) time complexity. When you sort the array you already are at O(n log n) to say the least

Log in to reply

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