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

  Рет қаралды 11,057

Greg Hogg

Greg Hogg

Күн бұрын

Пікірлер: 32
@GregHogg
@GregHogg 6 ай бұрын
IMPORTANT FIX - WE SHOULD LOOP THROUGH THE SET S AND NOT NUMS ON LINE 5!!!
@orlanduca2567
@orlanduca2567 5 ай бұрын
why is it relevant?
@khushgandhi1085
@khushgandhi1085 5 ай бұрын
@@orlanduca2567I think it’s because looping through the set allows for skipping any duplicates in nums, which would improve the performance.
@ffy291
@ffy291 4 ай бұрын
@@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
@franciscoserra8455
@franciscoserra8455 3 ай бұрын
@@orlanduca2567I tested the same code looping through the list and it took 434ms but looping through the set, which doesn’t have repeated numbers, took only 17ms.
@lautarortega
@lautarortega 3 ай бұрын
Does the set creation itself not count as an O(n) operation? You would need to iterate through all elements of the list to construct it. Then, you would be iterating over the elements twice, being O(n**2). Edit: Sorry, it would be O(2n), right? And the notation simplifies to O(n).
@aidanahram8245
@aidanahram8245 5 ай бұрын
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 4 ай бұрын
checking for the existence of a value in a set takes O(1) time. That's why we are using the set.
@SamuelSandeen
@SamuelSandeen 6 ай бұрын
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.
@GregHogg
@GregHogg 6 ай бұрын
Master Data Structures & Algorithms For FREE at AlgoMap.io!
@JoeTan-nq4fq
@JoeTan-nq4fq 3 ай бұрын
Awesome solution
@anuraag5487
@anuraag5487 6 ай бұрын
Very clean ❤❤
@SubhashCS-q8p
@SubhashCS-q8p 13 күн бұрын
can anyone please tell me why this is giving time limit exceeded error on leetcode? longest = 0 sett = set(nums) for num in nums: if num - 1 not in sett: max_len = 1 while num+1 in sett: max_len += 1 num += 1 longest = max(longest, max_len) return longest
@sajinreyans3329
@sajinreyans3329 4 ай бұрын
What is the use of longest =0 variable
@tominchazhikat3708
@tominchazhikat3708 5 ай бұрын
W Fellow right here
@fadygamilmahrousmasoud5863
@fadygamilmahrousmasoud5863 4 ай бұрын
thats smart
@cmarquay
@cmarquay Жыл бұрын
Hurray
@GregHogg
@GregHogg Жыл бұрын
😂
@chungt6545
@chungt6545 11 ай бұрын
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 11 ай бұрын
My Thoughts exactly, I mean there are two loops in there
@mohammadhammoud6531
@mohammadhammoud6531 9 ай бұрын
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 9 ай бұрын
@@mohammadhammoud6531 it is O(n), not O(n^2)
@chungt6545
@chungt6545 9 ай бұрын
@@mohammadhammoud6531 I think I got it finally. Thank you!
@mohammadhammoud6531
@mohammadhammoud6531 9 ай бұрын
​@@chungt6545 ur welcome 🙏🏻
@hlubradio2318
@hlubradio2318 8 ай бұрын
Very nicely done. You should be a college professor but most of them are cocky bastards anyway.
@ccricc-tf7fk
@ccricc-tf7fk 23 күн бұрын
Time limit Exceeded when you submit
@mouseaviator3672
@mouseaviator3672 20 күн бұрын
Iterate over each item in the set, not over each item in the list, this is because the list will have repetitive values, and you will continue computing the same sequence multiple times
@doomnypd
@doomnypd 10 күн бұрын
@@mouseaviator3672 Thanks got it
@hlubradio2318
@hlubradio2318 8 ай бұрын
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 8 ай бұрын
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 5 ай бұрын
wild 💀
Leetcode 128 - LONGEST CONSECUTIVE SEQUENCE
9:24
NeetCode
Рет қаралды 418 М.
«Жат бауыр» телехикаясы І 26-бөлім
52:18
Qazaqstan TV / Қазақстан Ұлттық Арнасы
Рет қаралды 434 М.
Counter-Strike 2 - Новый кс. Cтарый я
13:10
Marmok
Рет қаралды 2,8 МЛН
Coin Change - Leetcode 322 - Dynamic Programming (Python)
15:27
Majority Element - Leetcode 169 - Hashmaps & Sets (Python)
8:00
13 Python List Tricks Every Programmer Should Know
7:32
Coding Together
Рет қаралды 1,1 М.
Group Anagrams - Leetcode 49 - Hashmaps & Sets (Python)
7:28
Greg Hogg
Рет қаралды 18 М.
Combination Sum - Leetcode 39 - Recursive Backtracking (Python)
10:29
LONGEST CONSECUTIVE SEQUENCE | LEETCODE 128 | PYTHON SOLUTION
14:44
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 773 М.
Daily Temperatures - Leetcode 739 - Stacks (Python)
12:35
Greg Hogg
Рет қаралды 10 М.