Тёмный

Longest Consecutive Sequence - Leetcode 128 - Hashmaps & Sets (Python) 

Greg Hogg
Подписаться 188 тыс.
Просмотров 6 тыс.
50% 1

Master Data Structures & Algorithms for FREE at AlgoMap.io/
Code solutions in Python, Java, C++ and JS for this can be found at my GitHub repo here: github.com/gah...
Complete DSA Pathway Zero to Hero: • Data Structures & Algo...
Please check my playlists for free DSA problem solutions:
• Fundamental DSA Theory
• Array & String Questions
• 2 Pointers Questions
• Sliding Window Questions
• Binary Search Questions
• Stack Questions
• Linked List Questions
• Tree Questions
• Heap Questions
• Recursive Backtracking...
• Graph Questions
• Dynamic Programming (D...
My Data Science & ML RU-vid Playlist: • Greg's Path to Become ...
Learn Python and Data Science FASTER at mlnow.ai :)
Support the content: / @greghogg
Follow me on Instagram: / greghogg5
Connect with me on LinkedIn: / greghogg
Follow me on TikTok: / greghogg5
Coursera Plus: imp.i384100.ne...
My Favorite Courses:
Data Structures & Algorithms:
- UCalifornia San Diego DSA: imp.i384100.ne...
- Stanford Algorithms: imp.i384100.ne...
- Python Data Structures: imp.i384100.ne...
- Meta Coding Interview Prep: imp.i384100.ne...
Python:
- UMichigan Python for Everybody: imp.i384100.ne...
- Python Mastery from MLNOW.ai: mlnow.ai/cours...
- Google IT Automation w/ Python: imp.i384100.ne...
Web Dev / Full Stack:
- Meta Front-End Developer: imp.i384100.ne...
- IBM Full Stack Developer: imp.i384100.ne...
- Meta Back-End Developer: imp.i384100.ne...
- John Hopkins HTML, CSS & JS: imp.i384100.ne...
- IBM DevOps: imp.i384100.ne...
Cloud Development:
- AWS Fundamentals: imp.i384100.ne...
- GCP Cloud Engineer: imp.i384100.ne...
- Microsoft Azure Fundamentals: imp.i384100.ne...
Game Development:
- Michigan State Unity Development: imp.i384100.ne...
- UColorado C++ for Unreal Engine: www.coursera.o...
SQL & Data Science:
- SQL by MLNOW.ai: mlnow.ai/cours...
- Python for Data Science by MLNOW.ai: mlnow.ai/cours...
- Google Data Analytics: imp.i384100.ne...
- IBM Data Science: imp.i384100.ne...
- IBM Data Engineer: imp.i384100.ne...
Machine Learning & AI:
- ML Mastery at MLNOW.ai: mlnow.ai/cours...
- ML w/ Andrew Ng: www.coursera.o...
- Deep Learning w/ Andrew Ng: imp.i384100.ne...

Опубликовано:

 

16 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 24   
@GregHogg
@GregHogg Месяц назад
IMPORTANT FIX - WE SHOULD LOOP THROUGH THE SET S AND NOT NUMS ON LINE 5!!!
@orlanduca2567
@orlanduca2567 Месяц назад
why is it relevant?
@khushgandhi1085
@khushgandhi1085 Месяц назад
@@orlanduca2567I think it’s because looping through the set allows for skipping any duplicates in nums, which would improve the performance.
@ffy291
@ffy291 8 дней назад
@@orlanduca2567 skipping over duplicates. If not: if the start of a seq is a duplicated that exist k times you would check the seq k times which would bring you time up drasticlly
@SamuelSandeen
@SamuelSandeen 2 месяца назад
Interesting, my thought would be to remove elements from the set each time it was part of a sequence. Yours is likely more elegant since it doesn't mutate the set.
@aidanahram8245
@aidanahram8245 Месяц назад
This is O(n^2) because for each value in the set, you are finding the longest sequence that starts with that value. For example [1, 2, 3, 4, 5], it will find count 5, then 4, ..., to 1. Before you start the inside while loop you must check that it is the start of the sequence by making sure that the num - 1 isn't in the set. If its not in the set then start checking num + 1
@Infinitely16
@Infinitely16 11 дней назад
checking for the existence of a value in a set takes O(1) time. That's why we are using the set.
@anuraag5487
@anuraag5487 2 месяца назад
Very clean ❤❤
@sajinreyans3329
@sajinreyans3329 10 дней назад
What is the use of longest =0 variable
@tominchazhikat3708
@tominchazhikat3708 Месяц назад
W Fellow right here
@cmarquay
@cmarquay 8 месяцев назад
Hurray
@GregHogg
@GregHogg 8 месяцев назад
😂
@chungt6545
@chungt6545 7 месяцев назад
Thank you for the explanation. I'm not sure if I understand it correctly, but I think the time complexity is not O(n) but O(n^2), since we have 2 loops. The while loop makes the solution O(nk), where k is the average consecutive sequence's length. In the worst case, it's O(n^2), when all n elements construct an n-length consecutive sequence. How do you think?
@sonicrushXII
@sonicrushXII 7 месяцев назад
My Thoughts exactly, I mean there are two loops in there
@mohammadhammoud6531
@mohammadhammoud6531 5 месяцев назад
That's wrong , it is O(n) because you are iterating over each element exactly once . The worst case you are talking about is not O( n^2) it is O(n + n ) since you will run over all the elements once you reach the min of the sequence , but after that , every other element will have the element before already in the set , thus won't enter the 2nd loop. Hope this explains it.
@RashidAlipage
@RashidAlipage 5 месяцев назад
@@mohammadhammoud6531 it is O(n), not O(n^2)
@chungt6545
@chungt6545 5 месяцев назад
@@mohammadhammoud6531 I think I got it finally. Thank you!
@mohammadhammoud6531
@mohammadhammoud6531 5 месяцев назад
​@@chungt6545 ur welcome 🙏🏻
@hlubradio2318
@hlubradio2318 3 месяца назад
Very nicely done. You should be a college professor but most of them are cocky bastards anyway.
@GregHogg
@GregHogg Месяц назад
Master Data Structures & Algorithms For FREE at AlgoMap.io!
@hlubradio2318
@hlubradio2318 3 месяца назад
Holy cow! Aren't there Sr SE who'd architect the software? Why make us do things like this. I know people who never majored in CS and are Software Engineers. Unbelievable
@hlubradio2318
@hlubradio2318 3 месяца назад
Prof Singh Gupta. First day of class. Hi students! Are you smarter than me? If not you'll be in hell in my class. If you are you can be my student. What the f!
@technicallytechnical1
@technicallytechnical1 Месяц назад
wild 💀
Далее
Daily Temperatures - Leetcode 739 - Stacks (Python)
12:35
ГОЧА ПРО NISSAN 400Z
00:51
Просмотров 38 тыс.
The LeetCode Fallacy
6:08
Просмотров 508 тыс.
Number of Islands - Leetcode 200 - Graphs (Python)
11:01
LeetCode 128 "HARD" - Longest Consecutive Sequence
10:10
Leetcode 128 - LONGEST CONSECUTIVE SEQUENCE
9:24
Просмотров 353 тыс.
LeetCode was HARD until I Learned these 15 Patterns
13:00
The unfair way I got good at Leetcode
6:47
Просмотров 445 тыс.
ГОЧА ПРО NISSAN 400Z
00:51
Просмотров 38 тыс.