Longest increasing subsequence

Sdílet
Vložit
  • čas přidán 22. 11. 2019
  • This video explains how to find both the longest increasing subsequence length along with the subsequence itself. This is the simplest explanation along with logic. This is a very frequently asked programming interview question. The code for reference is present in the description section below, so please check it out. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
    CODE LINK: drive.google.com/open?id=1Oks...

Komentáře • 189

  • @mrinalraj7166
    @mrinalraj7166 Před 3 lety +28

    You must be very proud of yourself for creating such great contents. Thanks. You are a big help. Keep up the good work. Our community needs you.

  • @amanrai8010
    @amanrai8010 Před 3 lety +7

    You are very interactive with us despite having a good number of views you make sure to clear each and everyone's doubt. Thank you.

  • @dev-skills
    @dev-skills Před 3 lety +22

    Great to see you also covered the variation of this problem where we need to return the subsequence itself instead of longest subsequence length.

  • @rishabhshukla5199
    @rishabhshukla5199 Před 2 lety +4

    What to say this series is just amazing ❤️

  • @jaswantrohila3776
    @jaswantrohila3776 Před 3 lety +8

    CZcamsrs have made soo much buildup for this problem 😂.....but you killed it man...🔥🔥 Thanks

  • @fanigo138
    @fanigo138 Před 3 lety +24

    Great explanation! Please upload the o(N*logN) approach.

  • @prodiptamondal1758
    @prodiptamondal1758 Před 4 lety +6

    Recursion with Memoization time complexity is also O(n^2)

  • @sandeeprajurs1994
    @sandeeprajurs1994 Před 3 lety +2

    Nice Explanation! I think its very difficult to solve this problem in interview with out prior knowledge of this approach.

  • @dev-skills
    @dev-skills Před 3 lety

    Solution to this problem and its limitations using Backtracking approach very well explained and was also very to the point.

  • @samlinus836
    @samlinus836 Před rokem

    Others explained how.....
    You explained how + why - That's great

  • @amandarash135
    @amandarash135 Před 4 lety +4

    Thank bro. I was just worried about the length of video. But now it doesn't matter.

  • @gouthamreddy5713
    @gouthamreddy5713 Před 4 lety

    is there any video of you eplaining lis in recursive manner though it is not optimal but recursion is important to do dp??

  • @rishabhkumar8115
    @rishabhkumar8115 Před 3 lety +4

    nice explanation. Thankx a lot for this.
    KEEP IT UP!!.

  • @RomanUnreal
    @RomanUnreal Před 9 měsíci

    Thanks! I understand !

  • @karansagar7870
    @karansagar7870 Před 4 lety

    how does dp improve solution we can iterate from right to left use stack and push in stack if num is less than top of stack store size of stack in max ,run loop each time 1 less than previous to 0 .

  • @creatorstudio5945
    @creatorstudio5945 Před 11 měsíci

    Thank you sir for creating such great content and helping us building our concepts.

  • @Sweety-rx8zq
    @Sweety-rx8zq Před 2 lety

    can someone please explain that LIS comes under overlapping subproblems or optimal substructure?

  • @ranjanreddy623
    @ranjanreddy623 Před rokem

    Explanation is clear as a crystal ⭐⭐

  • @vijayakumareyunni6010
    @vijayakumareyunni6010 Před rokem +1

    at 7:16, "If you think about it, you will get it yourself". Instead of calling out names of variables, arrays and reading out every number in the array, time should be spent wisely to explain the "intuition" behind the logic - That's more important than syntax.

  • @PankajKumar-ft7lc
    @PankajKumar-ft7lc Před 4 lety +16

    second part(finding the sub sequence) is wrong.
    what if the arr is [99,100,1,2,101,3]
    then lis array will be [1,2,1,2,3,3]
    now according to you LIS should be 1,2,3 or 99,2,3 or 99,100,3 or 1,2,101 or 99,2,101 or 99,100,101.
    and many of these are wrong.
    Please correct me if I am wrong or I miss understood anything.

    • @techdose4u
      @techdose4u  Před 4 lety +7

      Well, the thing which i did not tell is, you will keep comparing corresponding values in your original array before making a concrete decision of choosing a lower value as a LIS subsequence. If you don't compare corresponding elements in original array then there is no way to know the correct answer. Thanks for highlighting this imp point.

    • @anantjain1263
      @anantjain1263 Před 3 lety

      How to write logic for it , can u explain?

  • @Shakib.Siddiqui
    @Shakib.Siddiqui Před 6 měsíci

    Great what a good explanation and solution

  • @AhamedKabeer-wn1jb
    @AhamedKabeer-wn1jb Před 2 lety

    Amazing Sir..superb Explanation..

  • @meghanachandrayama3103
    @meghanachandrayama3103 Před 3 lety +1

    Hey can u please explain the method for calculating the subsequence itself more precise in a more general way. I guess we should start from the index max value of LIS array and traverse back for finding the index with 1less than the max value and along with that we even have to ensure that arr[i]

  • @yitingg7942
    @yitingg7942 Před 3 lety +4

    I got up at 5:30 to watch your videos lol... I love it, you explained it so well, I don't think I will ever forget about this solution. I didn't get it out myself cause I was stuck trying to get it in constant time and then I couldn't. Some DP can be done using linear some has to use N^2. So sometimes I got stuck not knowing how to think of the best solution. There's a follow up question to prove it to O(n log(n)) time as well. ehhh, binary search again.

    • @techdose4u
      @techdose4u  Před 3 lety

      5:30! ❤️ Please explain me NlogN solution. Wanna learn it from you :)

    • @yitingg7942
      @yitingg7942 Před 3 lety +1

      @@techdose4u Let me try to figure it out and I will teach you 🤣🤣

    • @techdose4u
      @techdose4u  Před 3 lety

      @@yitingg7942 Thanks ❤️ I am waiting for it 😀

    • @yitingg7942
      @yitingg7942 Před 3 lety +1

      @@techdose4u I tried Surya, I just couldn't understand the binary search solution nor the binary search code itself.

    • @techdose4u
      @techdose4u  Před 3 lety +2

      Okay. Then I will try to explain you now :)

  • @oreoshake5951
    @oreoshake5951 Před 2 lety +2

    For the variation. If we have array as 10, 5, 11, 6, 9
    Here the lis array would look like: 1, 1, 2, 2, 3
    But when we trace back for string of longest increasing subsequence then as said the array would go by taking 9 , then we could either take up 6 or 11, but taking 11 would be wrong subsequence.
    I dont know if I am missing something. Though thanks for the explanation.

    • @karthiknayak828
      @karthiknayak828 Před 2 lety +1

      We can probably add a condition where arr[i] < top of the stack if we are storing the values of the subsequence in the stack. Hope this helps

  • @kavinraj1543
    @kavinraj1543 Před 3 lety +2

    That's a really clear explanation ...Great

  • @spetsnaz_2
    @spetsnaz_2 Před 4 lety +2

    never thought that you would make this problem soooooo ezzzzzyyyyy.....😍😍😍

  • @sonal9kothari
    @sonal9kothari Před 3 lety +2

    I came across a question, which I feel is similar to LIS. Problem statement : find the longest subsequence, such that the XOR of adjacent elements in the subsequence is equal to a given ‘k’ value. Please throw some light on this problem statement.

  • @9-1939
    @9-1939 Před rokem

    💯 superb explanation

  • @prakharagarwal6237
    @prakharagarwal6237 Před 2 lety

    Amazing Explanation!

  • @reallydarkmatter
    @reallydarkmatter Před 3 lety +1

    You are really amazing, the best video for this question

  • @mandeep2192
    @mandeep2192 Před 4 lety +1

    very beautifully explained..the hardest concept

  • @ris_colid325
    @ris_colid325 Před 11 měsíci +1

    Nice explanation. Can you please make an explanation video for the optimal method using DP with binary search as well? It has n Log n time complexity. Also ,came across another problem asking us to find number of longest increasing subsequences. If you could explain on that ,it will be a great help as well .

  • @testbot6899
    @testbot6899 Před rokem +1

    class Solution:
    def lis(self, A):
    res = [A[0]]
    n = len(A)

    for num in A[1:]:
    if num>res[-1]:
    res.append(num)
    else:
    res[bisect_left(res,num)] = num
    return len(res)

  • @techamet428
    @techamet428 Před 3 lety +6

    really good explanation..but it would be more helpful if you also start with recursive approach and slowly convert it into a dp..if video length if the problem you can make it in parts..it would be more helpful for beginners like me to solve dp problems.... Overall a very good explanation..

    • @techdose4u
      @techdose4u  Před 3 lety

      Yep. I will make that from further videos :)

  • @ANKURSingh-yl2lj
    @ANKURSingh-yl2lj Před 3 lety +8

    can u make a video for this problem in O(nlogn) complexity by using dp+binarysearch by the way ur explanation is brilliant ur voice quality and picture quality is great thanx fr this type of help

    • @techdose4u
      @techdose4u  Před 3 lety +6

      Welcome. I will try in future as I get time.

    • @275phuongvy
      @275phuongvy Před 3 lety

      @@techdose4u still waiting for this video =]]

  • @ganeshkamath89
    @ganeshkamath89 Před 2 lety

    When I paused to think, I solved it differently. I sorted the elements, then applied uncrossed line (LCS). It is giving same answer.

  • @hitengoyal5457
    @hitengoyal5457 Před 3 lety +4

    There is a binary search approach also which is O(nlogn)

  • @ajaywadhwa3398
    @ajaywadhwa3398 Před 3 lety +1

    Wow !! Really Awesome Dude.

  • @dhanashreegodase4445
    @dhanashreegodase4445 Před 2 lety

    Thanks again.....

  • @AmanKumar-ht8xi
    @AmanKumar-ht8xi Před 4 lety +8

    please make videos on some of the questions of "must do interview preparation" from geeksforgeeks.. because it has quality questions and solution is not available on youtube.. by the way your explanation is just awesome.. thank you for the videos

    • @techdose4u
      @techdose4u  Před 4 lety +7

      That's my objective too. I will be covering the most important questions in coming 6 months.

    • @divasbhadani9225
      @divasbhadani9225 Před 4 lety

      @@techdose4u Thanks.
      Your videos are really soo helpful.

    • @mrinalraj7166
      @mrinalraj7166 Před 3 lety

      @@techdose4u Yes please do that. You are helping the community.

  • @jvmilgrau5793
    @jvmilgrau5793 Před rokem

    Nice job @TECH DOSE

  • @salmaharb4588
    @salmaharb4588 Před měsícem

    great video.

  • @pawanlok1776
    @pawanlok1776 Před rokem

    thank you ...

  • @MohitKumar-ub2td
    @MohitKumar-ub2td Před 4 lety +4

    One of the best explanation of this topic i have seen on CZcams.
    Keep going man and your way of teaching the concept is too good and also you make it easy to understand...

    • @techdose4u
      @techdose4u  Před 4 lety

      Thanks :)

    • @MohitKumar-ub2td
      @MohitKumar-ub2td Před 4 lety +1

      You deserve it, honestly speaking i merely write anything but when someone makes such content then i can't stop myself because my inner soul says that this man has putting lots of hard work for making such content and futher it helps a lots of students.....

    • @MohitKumar-ub2td
      @MohitKumar-ub2td Před 4 lety

      Also please make more and more videos...

    • @techdose4u
      @techdose4u  Před 4 lety +5

      I do take out time on weekend for teaching. That's my hobby :)

  • @bharatiyadharohar
    @bharatiyadharohar Před 3 lety +2

    Best explanation from basic to very next level. Awesome 👌👌🔥🔥

  • @kishanmishra9802
    @kishanmishra9802 Před 3 lety

    Nice explanation

  • @renon3359
    @renon3359 Před 3 lety +3

    Thank you Surya, was finally able to understand this.

  • @aradhayaarora6155
    @aradhayaarora6155 Před 3 lety +3

    Can you please make a video on this problem with binary search and dp and O(nlogn) complexity with c++ code? Thank you :)

    • @techdose4u
      @techdose4u  Před 3 lety +1

      I will make it later. Currently doing heap.

  • @sunneykumar4560
    @sunneykumar4560 Před 4 lety +2

    what is the time complexity of that code ? is it o(nlog(n))?

  • @JangBahadur3028
    @JangBahadur3028 Před rokem

    code of Above logic : LEETCODE 300
    public static int lengthOfLIS(int[] nums) {
    int[] lis = new int[nums.length];
    // each element itself is an increasing sub sequence
    for (int i = 0; i < lis.length; i++) {
    lis[i] = 1;
    }
    for (int i = 0; i < nums.length; i++) {
    for (int j = 0; j nums[j] && lis[i] max) {
    max = Math.max(max, lis[i]);
    }
    }
    return max;
    }

  • @ServingTech
    @ServingTech Před 2 lety +1

    We can sort the sequence and find lcs of the sorted and original array.. i guess it will also work here...

  • @barathkumar7940
    @barathkumar7940 Před 3 lety

    Why can't we move from left to right to find the sub-sequence?

  • @dev-skills
    @dev-skills Před 3 lety +1

    1:45 subarray (not substring as we have integers not characters)

  • @astitvasrivastav9484
    @astitvasrivastav9484 Před 4 lety +2

    is it bad if i m not able to think for the algo myself ?

    • @techdose4u
      @techdose4u  Před 4 lety +3

      I was not able to do it myself. People can't do without having any practice. You will need to practice. Practice more and your intuition for it will increase. It works with everyone. Someone catches faster and someone late, but the point is, everyone catches. So try harder :)

    • @astitvasrivastav9484
      @astitvasrivastav9484 Před 4 lety

      @@techdose4u thanks a lot and continue your work becoz i know your views are going to rise exponentially they are too good!!

  • @piyushigarg8688
    @piyushigarg8688 Před 3 lety

    can you make a video on increasing decreasing and then increasing subsequence . fir e.x = a subsequence like ( 5 , 4, 19 )

  • @prateeksrivastava9
    @prateeksrivastava9 Před 4 lety +1

    Great Job!!!!Make More videos on DP.

  • @santasingh9045
    @santasingh9045 Před 4 lety +1

    great explanation sir!

  • @rajeshpanwar6796
    @rajeshpanwar6796 Před 2 lety

    thanks bro

  • @b.sainathreddy4567
    @b.sainathreddy4567 Před 4 lety +1

    Ur vedioes are just awesome

  • @rohandevaki4349
    @rohandevaki4349 Před rokem

    sir, do you have any frequently asked interview questions list??

  • @nabidulalam6956
    @nabidulalam6956 Před 3 lety +1

    clearly explained. thanks.
    btw one suggestion, if you could explain the memoization technique after the recursion it would be great lesson.
    recursion -> recursion + memoization -> DP -> DP(nlogn version) in that order would be a great help for the learners to catch the intuition behind every way to solve the problem.

  • @ganganagarimahender3187

    Why u use two for loops

  • @1989ashok
    @1989ashok Před 4 lety

    Which graphic tablet u r using to make this video ?

  • @willturner3440
    @willturner3440 Před 3 lety

    Please also upload video on binary search approach

  • @AnjaliKumari-zm9zo
    @AnjaliKumari-zm9zo Před 4 lety +2

    really nice video. but can you please upload a video for time complexity nlogn?

    • @techdose4u
      @techdose4u  Před 4 lety +1

      Yea sure :) Will take your advice in consideration as well.

  • @pranjalgupta9427
    @pranjalgupta9427 Před 3 lety +2

    Thanks surya 😃😃

  • @devendunegi247
    @devendunegi247 Před 7 měsíci

    would have been great if you deduced this from the memoized solution of LIS

  • @srinivasgangaraju2998

    16:39 for getting the subsequence array

  • @288Somaanil
    @288Somaanil Před 3 lety +4

    Can you cover the solution with n(log n) complexity?

  • @akshatjain6854
    @akshatjain6854 Před 4 lety +2

    Bro you are superb. Apart from uploading coding videos. Please try to make videos on general topics while preparing for interviews like how to dry run the code effectively etc.

    • @techdose4u
      @techdose4u  Před 4 lety

      Good idea....but this can only be done from the next month as I am busy with leetcod this month.

    • @akshatjain6854
      @akshatjain6854 Před 4 lety

      @@techdose4u okay bro

    • @akshatjain6854
      @akshatjain6854 Před 4 lety +1

      @@techdose4u Bro if I just practice the most popular 30-40 questions of each topic , will it be enough to crack interviews?

    • @techdose4u
      @techdose4u  Před 4 lety

      Yes.... That should be enough.... Provided you keep practicing and keep learning

    • @akshatjain6854
      @akshatjain6854 Před 4 lety

      @@techdose4u ok bro

  • @PritamKumar-mr5dv
    @PritamKumar-mr5dv Před 3 lety +1

    great video.....

  • @rohandevaki4349
    @rohandevaki4349 Před rokem

    there is no explanation for this algorithm in the description

  • @adityadhikle9473
    @adityadhikle9473 Před 3 lety +1

    Great 💯💯💯🙌🙌

  • @parasjain9416
    @parasjain9416 Před 3 lety +1

    Dhanyawad

  • @ayushupadhyaya7656
    @ayushupadhyaya7656 Před 3 lety +1

    Please also cover the O(nlogn) TC approach

  • @amarjeetyadav26436
    @amarjeetyadav26436 Před rokem

    its not giving correct output for printing lis

  • @istvanszabo6875
    @istvanszabo6875 Před rokem

    Nice explanation!
    Could the condition be more restrictive?
    Sth like: arr[i] > arr[j] && lis[i] == lis[j]

  • @shreyyy_marwaha
    @shreyyy_marwaha Před 3 lety +5

    Not very efficient algorithm. Its O(n^2).....can be done in nlogn. But still nice explanation 👍.

    • @techdose4u
      @techdose4u  Před 3 lety

      👌🏼

    • @shreyyy_marwaha
      @shreyyy_marwaha Před 3 lety +1

      @@techdose4u can you please make a video on nlogn solution also?

    • @techdose4u
      @techdose4u  Před 3 lety +1

      I will make it later. I am trying to cover the topics which are left.

    • @devendradahale546
      @devendradahale546 Před 3 lety

      Just we need to search from 0 to i using binary search

  • @komalbhatia8986
    @komalbhatia8986 Před 3 lety +1

    Thank you sir

  • @gautamgoel2024
    @gautamgoel2024 Před 2 lety

    This can be done n*logn time with binary search. that's better with time complexity and easy to understand.

  • @jitendarsahani11
    @jitendarsahani11 Před 4 lety +1

    What will you do in element are repeated?

    • @techdose4u
      @techdose4u  Před 4 lety

      We don't include Repeating elements in LIS.

  • @souravdebnath7732
    @souravdebnath7732 Před rokem

    Sir can share the algorithm pls

  • @kpclasses7985
    @kpclasses7985 Před 3 lety +1

    good explanation

  • @samgedam0
    @samgedam0 Před 3 lety

    Can anyone help me to store both the longest subsequences.

  • @anonymous-sk2pr
    @anonymous-sk2pr Před 2 lety +2

    ❤️❤️❤️

  • @mohsinkhan845
    @mohsinkhan845 Před 3 lety +1

    nice explanation

  • @anuragalvala2113
    @anuragalvala2113 Před 3 lety +2

    can we reduce the time complexity to O(nlogn)?

    • @techdose4u
      @techdose4u  Před 3 lety

      Yes it's possible.

    • @anuragalvala2113
      @anuragalvala2113 Před 3 lety +2

      @@techdose4u can you make a video on that approach?

    • @vaibhavkumar9192
      @vaibhavkumar9192 Před 3 lety

      @@techdose4usir, please make a video

    • @techdose4u
      @techdose4u  Před 3 lety +1

      @@vaibhavkumar9192 I don't really know if people will get it :P But I will try it after completing the basic DSA series for placement :)

  • @uditkumar867
    @uditkumar867 Před 4 lety +1

    I am so weak in DP .. I attempted 15 questions and never able to think the DP approach by myself always have to look at the solution.

    • @techdose4u
      @techdose4u  Před 4 lety +3

      Atleast try to understand from solutions or editorial. As long as you do it, you will be able to solve soon.

  • @eastwoodsamuel4
    @eastwoodsamuel4 Před 3 lety

    Solution required with nLogn time complexity

  • @SaurabhSuman-wz1ex
    @SaurabhSuman-wz1ex Před 3 lety

    it should be done in nlogn time complexity

  • @shivomkumar628
    @shivomkumar628 Před rokem

    what do you mean by you will figure it out ? the main condition that need explaining you said you will figure it out. seems like you are just copying the code and saying it will work.

  • @hemanthram4369
    @hemanthram4369 Před 3 lety +1

    pls do a video exclusive for basic code of backtracking

    • @techdose4u
      @techdose4u  Před 3 lety +1

      I will do it in recursion series when I start it.

  • @rakshith3547
    @rakshith3547 Před 3 lety +1

    could you please do even 673. Number of Longest Increasing Subsequence problem ?

  • @samgedam0
    @samgedam0 Před 3 lety

    The problem is I want to find greatest sum. So i must get and compare both the longest subsequence .

  • @sanjeevayyagari7060
    @sanjeevayyagari7060 Před 3 lety

    why should we check lis[ i ]

  • @Pooh__7__
    @Pooh__7__ Před 3 lety +2

    the longest increasing subsequence can also be 5,8,7,9

    • @Pooh__7__
      @Pooh__7__ Před 3 lety +1

      @@harshyadav5162 sorry I posted it when I hadn't learned abt it

    • @harshyadav5162
      @harshyadav5162 Před 3 lety

      @@Pooh__7__peace ✌️

  • @amizhthanmadan4647
    @amizhthanmadan4647 Před 2 lety

    You should have explained the second part of the if condition, just saying think about it doesnt equate to teaching.

  • @Relaxingmusic-tw4qn
    @Relaxingmusic-tw4qn Před 3 lety +1

    bro your videos are quite awesome you can even charge minimum for its access by making it more orderly,, its pains me to see how shit like white hat jr are charging so much money for the same level contend. education should be free and open to every1

  • @abhinavghosh725
    @abhinavghosh725 Před 4 lety

    from 14:00 to 14:20 is the main part