Longest Consecutive Sequence - LeetCode 128 - Java

preview_player
Показать описание
This is the complete guide to solve Longest Consecutive Sequence question on LeetCode. We discuss 2 brute force approaches then we will give a detailed explanation/code the O(n) optimization!

Video Contents:

00:00 - Read and understand the question
02:38 - Brute Force 1 explained (sorting)
03:58 - Brute Force 2 explained - important!
07:09 - Optimization, O(n), explained
14:19 - Code/further explain the solution
22:37 - Time and Space complexity
Рекомендации по теме
Комментарии
Автор

I swear that you provide one of the highest, if not the highest, quality explanations on YouTube! Thank you so much for this. I understood each problem you uploaded on this channel completely!

suggestaname
Автор

Thank you for taking the time to do this. I saw your post on reddit and came over to check out the channel! Great work and goodluck.

balu
Автор

You could use a regular for loop (int i = 0; i < nums.length;) instead of the foreach loop and increment the loop control variable, `i` after you have incremented the length and similarly in an else case - this way you can directly skip the numbers in the outer loop where you do not see a sequence starting and getting a better runtime.

HarshitShrivastava
Автор

thank you so super easy to understand :)

anishaa
Автор

Approach #3: For an array with duplicates e.g. nums = [0, 0, 0, 1, 4, 3, 2] we'd generate the sequence <0, 1, 2, 3, 4> three times!

floatingfortress
Автор

why you used while loop in line 11 can you explain this?

wajihakhan
Автор

Isn't time complextity worst case scenario? Worst case for this code is 2 loops for each num without a sequence since we always start at num + 0. I don't understand how this is O(n) time complexity

mirriammaina