Longest Consecutive Sequence - Complete Solution Guide
Longest Consecutive Sequence is LeetCode problem 128, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 Example 3: Input: nums = [1,0,1,2] Output: 3 Constraints: 0 <= nums.length <= 10 5 -10 9 <= nums[i] <= 10 9
Detailed Explanation
The problem asks us to find the length of the longest consecutive sequence of integers within an unsorted array of integers. The consecutive sequence doesn't need to be contiguous in the input array. The crucial requirement is that the solution must run in O(n) time complexity. For example, given the input [100, 4, 200, 1, 3, 2], the longest consecutive sequence is [1, 2, 3, 4], so the output should be 4.
Solution Approach
The provided solution utilizes a set to efficiently check for the presence of numbers. The algorithm iterates through the numbers in the set. For each number, it checks if the number minus one is also in the set. If not, this indicates that the current number is the potential start of a consecutive sequence. The algorithm then iterates upwards from this number, checking for consecutive numbers and incrementing a streak counter until a break in the consecutive sequence is found. The longest streak seen so far is updated accordingly.
Step-by-Step Algorithm
- Step 1: Create a set from the input array `nums`. This removes duplicates and allows for O(1) lookups.
- Step 2: Initialize a variable `longest_streak` to 0 to store the maximum consecutive sequence length found so far.
- Step 3: Iterate through each `num` in the set.
- Step 4: For each `num`, check if `num - 1` is present in the set. If it is not present, this means `num` is the potential start of a new consecutive sequence.
- Step 5: If `num - 1` is not present, initialize `current_num` to `num` and `current_streak` to 1.
- Step 6: While `current_num + 1` is present in the set, increment `current_num` and `current_streak`.
- Step 7: After the inner while loop finishes, update `longest_streak` to the maximum of `longest_streak` and `current_streak`.
- Step 8: After iterating through all numbers in the set, return `longest_streak`.
Key Insights
- Insight 1: Using a set data structure allows for O(1) average time complexity for checking the existence of an element, which is essential for achieving the O(n) time complexity requirement.
- Insight 2: To avoid redundant checks, we only start counting a sequence from the smallest number in that sequence (i.e., a number where `num - 1` is not present in the set).
- Insight 3: Although there is a while loop within the for loop, each number is only visited once as the start of the sequence. This ensures the O(n) time complexity.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table, Union Find.
Companies
Asked at: Atlassian, ByteDance, Cisco, DE Shaw, DeltaX, EPAM Systems, Flipkart, Google, IBM, Infosys, Lyft, Oracle, PayPal, Paytm, PhonePe, Roblox, SAP, ServiceNow, Swiggy, TikTok, Turing, UKG, Uber, Walmart Labs, Wissen Technology, Yahoo, Yandex, Zepto, Zoho, tcs.