🚀 LeetCode 128: Longest Consecutive Sequence | O(n) HashSet Solution | Python | DSA Day 20 🔥
🚀 LeetCode 128: Longest Consecutive Sequence | HashSet | O(n) Optimal Solution | Python | DSA Day 20 🔥 Day 20 of my DSA in Python Series! In this video, we solve LeetCode 128 – Longest Consecutive Sequence using the optimal HashSet approach that runs in O(n) time. Many beginners first think of sorting the array, but interviews expect an O(n) solution. In this video, you'll learn the intuition behind using a HashSet to find the longest consecutive sequence efficiently. 🐢 Approach 1: Sorting (Brute Force) class Solution: def longestConsecutive(self, nums): if not nums: return 0 nums.sort() longest = 1 current = 1 for i in range(1, len(nums)): if nums[i] == nums[i - 1]: continue elif nums[i] == nums[i - 1] + 1: current += 1 else: longest = max(longest, current) current = 1 return max(longest, current) ⏱ Time Complexity: O(n log n) 📦 Space Complexity: O(1) (excluding sorting space) ⚡ Approach 2: HashSet (Optimal) class Solution: def longestConsecutive(self, nums): num_set = set(nums) longest = 0 for num in num_set: if num - 1 not in num_set: current = num length = 1 while current + 1 in num_set: current += 1 length += 1 longest = max(longest, length) return longest ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) 📚 What You'll Learn ✅ Longest Consecutive Sequence Explained ✅ HashSet in Python ✅ Why we check num - 1 ✅ Start of Sequence Concept ✅ Sorting vs HashSet Approach ✅ Interview Optimization ✅ Time & Space Complexity Analysis ✅ Python DSA Interview Questions If this video helps you, please Like 👍, Share 🚀, Comment 💬, and Subscribe 🔔 Bharath Builds for more Python DSA, Coding Interview, and Placement Preparation content. 📌 Hashtags #LeetCode128 #LongestConsecutiveSequence #HashSet #Python #PythonDSA #DSA #CodingInterview #Algorithms #DataStructures #InterviewPreparation #PythonProgramming #LeetCode #Programmer #bharathbuilds 🎥 DSA in Python Playlist • DSA in Python ಕನ್ನಡದಲ್ಲಿ | Top 100 LeetCod... 💻 LeetCode Profile https://leetcode.com/u/BHARATH_BHUVI/ ⭐ GitHub https://github.com/Bharathkrishnamurthy 💼 LinkedIn / bharathk5052

Please Master This MAGIC Python Feature... 🪄

🚀 LeetCode 15: 3Sum | Brute Force vs HashSet vs Two Pointers | Python | DSA Day 26 🔥

From Child Prodigy to Winning Fields Medal, Nobel of Math

MAJORITY ELEMENT 😱DON'T USE HASHMAP!O(1) SPACE 🔥LeetCode 169 | Day 20

🚀 LeetCode 977: Squares of a Sorted Array | Two Pointers in Python | DSA Day 22 Explained in kannada

Views, Indexes & Constraints in SQL |DBMS Placement Series Day 20 | SQL Interview Questions |Kannada

10 Important Python Concepts In 20 Minutes

The Dirty AI lie : How the GREATEST bet in human history started to crack in June 2026?

The Computer Science Dream is Dead

Python Encapsulation Made Easy 🔒 | Build a Bank Account System | OOP Placement Series 2/5

High Salary. SH*T Life.

Learn Python OOP in under 20 Minutes

Don't Waste 2026 on the Wrong Career (Software vs AI Engineer)

LeetCode was HARD until I Learned these 15 Patterns

How To Learn So Fast It’s Almost Unfair

How I would learn Leetcode if I could start over

Python for Coding Interviews - Everything you need to Know

🚀 Transactions & ACID Properties in DBMS | Placement Questions Explained | Day 21/21

If You Have A Bad Memory, I’ll Help You Fix It In 28 Minutes

