FAANG · LeetCode #128
Longest Consecutive Sequence — three-stage drill
1The ProblemMedium
Longest Consecutive Sequence
Length of the longest run of consecutive integers, in O(n).
Examples
[100,4,200,1,3,2]→ 4[0,3,7,2,5,8,4,6,0,1]→ 9
constraintUnsorted; must run in O(n) — so sorting is off the table.
2The PatternTIME O(n) · SPACE O(n)
Arrays & Hashing
Trade space for time: a hash map turns 'have I seen this?' into an O(1) lookup.
Problems using this pattern
3The SolutionRESULT → 4
Step through it
100 → is 99 in the set? no → a run starts here · count 100… → length 1
Step 1 / 7
Arrow keys to navigate · escape to reset