Longest Consecutive Sequence - Solution | Hashmap and Heap | Data Structure and Algorithms in JAVA

preview_player
Показать описание

NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. In this video, we explain the solution for gettingLongest Consecutive Sequence in Hashmap.

#pepcoding #java #programming

Рекомендации по теме
Комментарии
Автор

Sir, your teaching method is just like Irrfan Khan's acting.
It's just effortless.
You make look difficult problems look easy to solve.
Thank you.

sudiptasundar
Автор

You make explaination look very very easy thanks much appreciated

simrankak
Автор

Its all love, love, and love 💖💖💖💖💖💖💖💖💖.
I have never seen ur full video but, still able to solve problems. Do you know what does this means?
It means that you are a great teacher and you explain things so simply. I just love it.
Thank You 3000

shishankrawat
Автор

I was learning dsa in java and struggling in a hasmap . But after watching this video now able to solve the many problem using hashing.
THANK YOU SO MUCH SIR...❤

viraldevmurari
Автор

Wow man! This problem looked very complicated at first but as you explain it - It just kept getting easier. Every single concept taught by you is unforgettable - <3. thanks again. Please keep up the good work. you're helping so muchhhh!!

udaykulkarni
Автор

The explanation for time complexity can't get better than this. Period.

bloody
Автор

Sir u completes the definition of a teacher..🙇‍♀

sanyamkothari
Автор

Thanks sir. Bahut accha kaam karte hai aap. The way with which you present your explanations
and give sound logic for each is something which you only possess.

sachinsharma
Автор

made it way easy, thank u sir ❤️
Love From Kashmir 😍

reshihashim
Автор

Amazing sir👏👏.
The way of teaching is
so unique and very easy way.🙏🙏

mohdzeeshan
Автор

Sir in worst case I think the third loop will not execute for exactly n times. It will execute for (2*n-1) times if there is only one consecutive sequence.
For example [4, 5, 7, 6, 8, 9, 10] -> 7 times for loop will get executed for each element of array...and 6 times inner while loop will get executed for array element 4.

Although it will not make any difference overall TC will remain O(n) but i just want to confirm that what I am thinking is correct or not.

Sir your teaching method is very good after seeing your lectures all concepts gets crystal clear.
ThankYou for such wonderful content.

RishiMishra
Автор

python code if someone needs:

class Solution:
def longestConsecutive(self, arr: List[int]) -> int:
d = {}
res = []
for i in arr:
d[i]=True
for i in d:
if (i-1) in d:
d[i] = False
max_len = 0
max_start = 0
for i in d:
if d[i] == True:
temp_length = 1
temp_start = i
while ((temp_length + temp_start) in d):
temp_length+=1
if temp_length>max_len:
max_start = temp_start
max_len = temp_length
for i in range(max_len):
res.append(max_start+i)

return len(res)

AnkurSingh-mkrc
Автор

Here is another solution using which has less time complexity:

int ans = 1;
HashSet<Integer> hashSet = new HashSet<>();
for (int num: nums) { // nums is the array
hashSet.add(num);
}

for (int val: hashSet) { //Looping through hashSet
if (!hashSet.contains(val-1)) {
int maxStreak = 1;
while (hashSet.contains(val+1)) {
maxStreak++;
val++;
}
ans = Math.max(ans, maxStreak);
}
}
return ans;

S.Saurabh_
Автор

Sir at 14:12 you said "while ne 'n' times contribute kiya". Then in for loop isn't time complexity will become n*n (n for for loop and n for while loop)?

shubhamsood
Автор

awesome explanation.Just a small clarification.The consecutive sequence can be in any order is true but what if the consecutive sequence has duplicates like for the above example say [100, 1, 200, 1, 10, 4, 0, 3, 8, 2].Here the consecutive sequence is [0, 1, 1, 2, 3, 4] so i need to store the frequency also correct if input has duplicates?

prasantharavindramesh
Автор

even aapne loop ke andr loop use kiya hai atleast wo element ko check krega ki next wala hai ya nhi hai to map me ksi bhi element ko search krne me hi o(log n) complexity lagti hai to n kaise hui??

sukanyasinha
Автор

Sir leetcode par submit karne par 69/70 tc pass ho rhe hain. Ek test case main TLE aa rha hai

aakashgoswami
Автор

isme duplicate element cover nahi ho payenge sir. Sari element distinct is prerequisite I assume.

rahulranjan
Автор

sir map ka creation bhi to time complexity O(n * Log n) letta hai...

inspired_enough_to_grow_up
Автор

Time complexity of this solution I think is: O(n*h) ; h = length of maximum consecutive sequence. Not O(n)

Any suggestions?

mrmagician