Java Solution (4ms) but complexity O(n*logn) with sort


  • 0
    G
    class Solution {
        public int longestConsecutive(int[] nums) {
            if (nums == null || nums.length == 0) return 0;
            
            int actualCount = 1;
            int max = 1;
            Arrays.sort(nums);
            
            for (int i = 1; i < nums.length; i++) {
                if (nums[i] == nums[i-1] + 1) {
                    actualCount++;
                    max = Math.max(max, actualCount);
                } else if (nums[i] == nums[i-1]) {
                    continue;
                } else {
                    actualCount = 1;
                }
            }
            return max;
        }
    }
    

Log in to reply
 

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