Longest Turbulent Subarray | Leetcode 978 | Live coding session 🔥🔥🔥

preview_player
Показать описание
Here is the solution to "Longest Turbulent Subarray" leetcode question. Hope you have a great time going through it.

1) 0:00 Explaining the problem out loud
2) 1:10 Algorithm walkthrough
3) 2:00 Solution approach
4) 10:00 Coding

For discussion/feedback

PS : Please increase the speed to 1.25X

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

Congratulations on 3k subscribers, long journey ahead :)

priyadarshanr
Автор

thank you for this conceptual overview

giannizamora
Автор

I have a dp approach to this problem,
Let there be 2 dp states for a given ith index
dp[i][0] denotes the length of turbulent subarray ending at i such that it's last comparison is (a[i]<a[i-1])
dp[i][1] denotes the length of turbulent subarray ending at i such that it's last comparison was (a[i]>a[i-1])
dp[i][0] = dp[i-1][1]+1
dp[i][1] = dp[i-1][0] +1
Then Dp[i] =max(dp[i][0], dp[i][1])
Take max of all Dp[i] and that will be our ans

randomnoob
Автор

I liked the video you posted yesterday interviewing abhishek jain, Great content!

rishabhms
Автор

now its very easy to understand . thanks

NitinSinghMinVlogs
Автор

Thank you for the amazing explanation sir😊

peeckle
Автор

can someone explain the reason why I am getting TLE while coding in cpp

muskangupta
Автор

Can you also tell the DP approach? which is the most important approach

SHASHANKRUSTAGII
Автор

i believe at 12:18 the condition 1st is of mountain and condition 2nd is of valley. correct me if i am wrong

naztabassum
Автор

can you please tell why (start +1 < n), (end +1 < n ) is understandable

priyadarshanr
Автор

I'm getting a TLE in the C++ code. Any idea?


class Solution {
public:
bool isTurbulent(int k, vector<int> A){
if(a[x]>a[x-1] && a[x]>a[x+1]) return 1;
if(a[x]<a[x-1] && a[x]<a[x+1]) return 1;
return 0;
}

int a) {
if(a.size()<2) return a.size();
int ans = 1; //if all values in the array are same. else minimum answer is 2
int start =0, end=0;
while(start<a.size()-1){
if(a[start]==a[start+1]){
start++;
continue;
}
end = start+1;
while(end+1<a.size() && isTurbulent(end, a)) end++;
ans = max(ans, end-start+1);
start = end;
}
return ans;
}
};

dhanyaabharadwaj
Автор

har problem tu kaise solve karta hain ladke mein ye problem samajh nai paya re

sureshchaudhari
welcome to shbcf.ru