Class Solution
Medium
Given an unsorted array of integers nums
, return the length of the longest consecutive elements sequence.
You must write an algorithm that runs in O(n)
time.
Example 1:
Input: nums = [100,4,200,1,3,2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]
. Therefore its length is 4.
Example 2:
Input: nums = [0,3,7,2,5,8,4,6,0,1]
Output: 9
Constraints:
0 <= nums.length <= 105
-109 <= nums[i] <= 109
To solve the “Longest Consecutive Sequence” problem in Java with a Solution
class, we’ll use a HashSet and a greedy approach. Below are the steps:
-
Create a
Solution
class: Define a class namedSolution
to encapsulate our solution methods. -
Create a
longestConsecutive
method: This method takes an arraynums
as input and returns the length of the longest consecutive elements sequence. -
Initialize a HashSet: Create a HashSet named
numSet
to store all the numbers in the arraynums
. -
Iterate through the array: Add all the numbers from the array
nums
to thenumSet
. -
Find the longest sequence: Iterate through the array
nums
again. For each numbernum
in the array:- Check if
num - 1
exists in thenumSet
. If it does not,num
could be the start of a new sequence. - If
num - 1
does not exist, start a new sequence fromnum
. IncrementcurrentNum
by 1 and check ifcurrentNum
exists in thenumSet
. Keep incrementingcurrentNum
until it does not exist in thenumSet
. Update the maximum length of the sequence accordingly.
- Check if
-
Return the maximum length: After iterating through the entire array, return the maximum length of the consecutive sequence.
Here’s the Java implementation:
import java.util.HashSet;
class Solution {
public int longestConsecutive(int[] nums) {
HashSet<Integer> numSet = new HashSet<>();
for (int num : nums) {
numSet.add(num); // Add all numbers to HashSet
}
int maxLength = 0;
for (int num : nums) {
if (!numSet.contains(num - 1)) { // Check if num - 1 exists in numSet
int currentNum = num;
int currentLength = 1;
while (numSet.contains(currentNum + 1)) { // Increment currentNum until it does not exist in numSet
currentNum++;
currentLength++;
}
maxLength = Math.max(maxLength, currentLength); // Update maximum length
}
}
return maxLength; // Return the maximum length of the consecutive sequence
}
}
This implementation follows the steps outlined above and efficiently calculates the length of the longest consecutive elements sequence in Java.
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
longestConsecutive
public int longestConsecutive(int[] nums)
-