|
1 | 1 | ## 1. Brute Force |
2 | 2 |
|
| 3 | +### Intuition |
| 4 | + |
| 5 | +A consecutive sequence grows by checking whether the next number (`num + 1`, `num + 2`, …) exists in the set. |
| 6 | +The brute-force approach simply starts from every number in the list and tries to extend a consecutive streak as far as possible. |
| 7 | +For each number, we repeatedly check if the next number exists, increasing the streak length until the sequence breaks. |
| 8 | +Even though this method works, it does unnecessary repeated work because many sequences get recomputed multiple times. |
| 9 | + |
| 10 | +### Algorithm |
| 11 | + |
| 12 | +1. Convert the input list to a set for **O(1)** lookups. |
| 13 | +2. Initialize `res` to store the maximum streak length. |
| 14 | +3. For each number `num` in the original list: |
| 15 | + - Start a new streak count at 0. |
| 16 | + - Set `curr = num`. |
| 17 | + - While `curr` exists in the set: |
| 18 | + - Increase the streak count. |
| 19 | + - Move to the next number (`curr += 1`). |
| 20 | + - Update `res` with the longest streak found so far. |
| 21 | +4. Return `res` after checking all numbers. |
| 22 | + |
3 | 23 | ::tabs-start |
4 | 24 |
|
5 | 25 | ```python |
@@ -178,6 +198,33 @@ class Solution { |
178 | 198 |
|
179 | 199 | ## 2. Sorting |
180 | 200 |
|
| 201 | +### Intuition |
| 202 | + |
| 203 | +If we sort the numbers first, then all consecutive values will appear next to each other. |
| 204 | +This makes it easy to walk through the sorted list and count how long each consecutive sequence is. |
| 205 | +We simply move forward while the current number matches the expected next value in the sequence. |
| 206 | +Duplicates don’t affect the result—they are just skipped—while gaps reset the streak count. |
| 207 | +This approach is simpler and more organized than the brute force method because sorting places all potential sequences in order. |
| 208 | + |
| 209 | +### Algorithm |
| 210 | + |
| 211 | +1. If the input list is empty, return `0`. |
| 212 | +2. Sort the array in non-decreasing order. |
| 213 | +3. Initialize: |
| 214 | + - `res` to track the longest streak, |
| 215 | + - `curr` as the first number, |
| 216 | + - `streak` as `0`, |
| 217 | + - index `i = 0`. |
| 218 | +4. While `i` is within bounds: |
| 219 | + - If `nums[i]` does not match `curr`, reset: |
| 220 | + - `curr = nums[i]` |
| 221 | + - `streak = 0` |
| 222 | + - Skip over all duplicates of `curr` by advancing `i` while `nums[i] == curr`. |
| 223 | + - Increase `streak` by `1` since we found the expected number. |
| 224 | + - Increase `curr` by `1` to expect the next number in the sequence. |
| 225 | + - Update `res` with the maximum streak found so far. |
| 226 | +5. Return `res` after scanning the entire list. |
| 227 | + |
181 | 228 | ::tabs-start |
182 | 229 |
|
183 | 230 | ```python |
@@ -413,6 +460,27 @@ class Solution { |
413 | 460 |
|
414 | 461 | ## 3. Hash Set |
415 | 462 |
|
| 463 | +### Intuition |
| 464 | + |
| 465 | +To avoid repeatedly recounting the same sequences, we only want to start counting when we find the **beginning** of a consecutive sequence. |
| 466 | +A number is the start of a sequence if `num - 1` is **not** in the set. |
| 467 | +This guarantees that each consecutive sequence is counted exactly once. |
| 468 | + |
| 469 | +Once we identify such a starting number, we simply keep checking if `num + 1`, `num + 2`, … exist in the set and extend the streak as far as possible. |
| 470 | +This makes the solution efficient and clean because each number contributes to the sequence only one time. |
| 471 | + |
| 472 | +### Algorithm |
| 473 | + |
| 474 | +1. Convert the list into a set `numSet` for O(1) lookups. |
| 475 | +2. Initialize `longest` to track the length of the longest consecutive sequence. |
| 476 | +3. For each number `num` in `numSet`: |
| 477 | + - Check if `num - 1` is **not** in the set: |
| 478 | + - If true, `num` is the start of a sequence. |
| 479 | + - Initialize `length = 1`. |
| 480 | + - While `num + length` exists in the set, increase `length`. |
| 481 | + - Update `longest` with the maximum length found. |
| 482 | +4. Return `longest` after scanning all numbers. |
| 483 | + |
416 | 484 | ::tabs-start |
417 | 485 |
|
418 | 486 | ```python |
@@ -597,6 +665,33 @@ class Solution { |
597 | 665 |
|
598 | 666 | ## 4. Hash Map |
599 | 667 |
|
| 668 | +### Intuition |
| 669 | + |
| 670 | +When we place a new number into the map, it may connect two existing sequences or extend one of them. |
| 671 | +Instead of scanning forward or backward, we only look at the lengths stored at the **neighbors**: |
| 672 | + |
| 673 | +- `mp[num - 1]` gives the length of the sequence ending right before `num` |
| 674 | +- `mp[num + 1]` gives the length of the sequence starting right after `num` |
| 675 | + |
| 676 | +By adding these together and including the current number, we know the total length of the new merged sequence. |
| 677 | +We then update the **left boundary** and **right boundary** of this sequence so the correct length can be retrieved later. |
| 678 | +This keeps the whole operation very efficient and avoids repeated work. |
| 679 | + |
| 680 | +### Algorithm |
| 681 | + |
| 682 | +1. Create a hash map `mp` that stores sequence lengths at boundary positions. |
| 683 | +2. Initialize `res = 0` to store the longest sequence found. |
| 684 | +3. For each number `num` in the input: |
| 685 | + - If `num` is already in `mp`, skip it. |
| 686 | + - Compute the new sequence length: |
| 687 | + - `length = mp[num - 1] + mp[num + 1] + 1` |
| 688 | + - Store this length at `num`. |
| 689 | + - Update the boundaries: |
| 690 | + - Left boundary: `mp[num - mp[num - 1]] = length` |
| 691 | + - Right boundary: `mp[num + mp[num + 1]] = length` |
| 692 | + - Update `res` to keep track of the longest sequence. |
| 693 | +4. Return `res` after processing all numbers. |
| 694 | + |
600 | 695 | ::tabs-start |
601 | 696 |
|
602 | 697 | ```python |
|
0 commit comments