Longest Increasing Subsequence

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ธ.ค. 2024

ความคิดเห็น • 282

  • @atishnarlawar1177
    @atishnarlawar1177 6 ปีที่แล้ว +46

    Its worth to appreciate the hard work. Any one can easily see him so exhausted after doing the daytime job at Apple. But he still makes these helpful videos. Kudos!

  • @Autom_te
    @Autom_te 5 ปีที่แล้ว +33

    This example was so clear and easy to follow that I can only wonder _how_ my professor did such a bad job at explaining this. Thank you!

  • @alfonsovalenciana436
    @alfonsovalenciana436 ปีที่แล้ว +4

    After all these years, I still come back to this channel. Always loved the clear way you explain!

  • @slowmotion2300
    @slowmotion2300 4 หลายเดือนก่อน +1

    No fancy animation, no BS music and intros, 100% explanation, You make it look like it's a piece of cake.

  • @anamigator
    @anamigator 7 ปีที่แล้ว +110

    Intuition behind this solution:
    If the current element is greater than the previous element, we can safely add 1 to the value of previous max sum. Since the previous max sum was also calculated in a similar manner (optimal substructure) we can safely add 1 to previous max value to get the current value.
    If you notice in case of 6, since it is greater than 0, you can add 1 to the max value at 0. But there is a catch, you can have previous elements (for example 4) which can give you a bigger subsequence. Hence to check this, every time the "j" begins with zero. Or you can even start at the given i and keep going backwards with a j variable to check if there are two or more contending elements which can give you the max value.
    Notice how we construct the solution from using the previous solutions (previous values in array). This is in essence Dynamic Programming where you remember the previous computed values and make calculations faster.
    PS: please correct mistakes if any.

  • @souvikghosh5668
    @souvikghosh5668 2 ปีที่แล้ว +7

    He is such a gem of a person. I don't know why he has stopped making such videos now. Wish he comes back again.

  • @ganapatibiswas5858
    @ganapatibiswas5858 4 ปีที่แล้ว +1

    For people who don't have much time to watch the entire video here is the solution
    0:45 --> "Yes we will have to use dynamic programming to solve this "

  • @SubhamKumar-eg1pw
    @SubhamKumar-eg1pw 8 ปีที่แล้ว +1

    Struggled whole day on a problem on alternating sequence. Then solved within 15 minutes after watching this. Thanks a lot :)

  • @treksis
    @treksis 3 ปีที่แล้ว +4

    This video is made 6 years ago and still one of the best

    • @semthree5736
      @semthree5736 3 ปีที่แล้ว +1

      Yes..I'm having 3rd semester now😄😨

    • @quantisticnumbers2633
      @quantisticnumbers2633 11 หลายเดือนก่อน

      yea it so crazy. 8 years later, I watched like 10 newer videos yet this is still the best explanation.

  • @MadForCs16
    @MadForCs16 4 ปีที่แล้ว +4

    There is a small optimization here (Not talking about DP):-
    Rather than starting j from index 0, start j from i-1 and then go backward. what difference will it make? see below:
    If let's say you're at index i = 500 Then you move from j=499 and at j=300 you find that its LIS is 200 and array[j] is lesser than array[i], then you make LIS of i=500 as 200+1 = 201. Now when you continue going backward i.e j=299,298,297.......then you do not need to go beyond j=200 because you already know that those elements can have LIS as 200 at max, So no need to compare.... This would save your 200 iterations which you had to do when you were starting j from 0 index.

  • @vikremsheker
    @vikremsheker 3 หลายเดือนก่อน

    Excellent explanation ! Thank you for taking the time to make these videos !

  • @abhishekgorisaria2897
    @abhishekgorisaria2897 9 ปีที่แล้ว +8

    All your videos are excellent, extremely Helpful.

  • @eduardopetry911
    @eduardopetry911 6 ปีที่แล้ว +2

    Great video! I wrote a function in Python that implements the algorithm Tushar exposed in the video.
    Hopefully this helps someone better understand how it works...
    def LIS(arr):
    j, i, t = 0, 1, [1] * len(arr) # initialize ints j, i and array t
    while i < len(arr): # loop while i is within the sequence's bounds
    if arr[j] < arr[i]: t[i] = max(t[i], t[j] + 1) # exact same logic Tushar wrote at the end of video
    j = j + 1 # at each iteration, increment j
    if j == i: j, i = 0, i + 1 # if j meets i, set j to 0 and increment i
    return max(t)

  • @RakeshGajjar
    @RakeshGajjar 8 ปีที่แล้ว

    I stugled to understand this problem and referred other sources as well. This video gave me a final push to clear my understanding. Thanks Tushar!

  • @Chelsea_Alexis
    @Chelsea_Alexis 8 ปีที่แล้ว +1

    Thank you so much for making these helpful videos!! I watched this whole playlist before my dynamic programming exam and I got an A on it.

  • @karthikmucheli7930
    @karthikmucheli7930 5 ปีที่แล้ว +51

    yes, we will have to use DP to solve this problem. :D:P

    • @baburayhanshaik5121
      @baburayhanshaik5121 4 ปีที่แล้ว +2

      XD

    • @Ebizzill
      @Ebizzill 4 ปีที่แล้ว

      staaahpp!!! 😂

    • @zhengrui315
      @zhengrui315 4 ปีที่แล้ว

      Actually a better solution is to use binary search, the time complexity becomes N logN, but its good to know DP

  • @slizverg23
    @slizverg23 2 ปีที่แล้ว

    Wow, that’s the first video on that subject there I finally understand the algorithm! Many thanks!:)

  • @lalitdudheria7955
    @lalitdudheria7955 8 ปีที่แล้ว

    We need more people like you Tushar. I have seen many of your videos. You explain really well :)

  • @ffles
    @ffles 9 ปีที่แล้ว

    Thank you so much for this video. The simple fact that you stepped through the algorithm was what I needed to understand what was going on. I don't know why so many teachers skip it!

  • @abnormal7273
    @abnormal7273 8 ปีที่แล้ว +246

    @Tushar: Just a suggestion, it would be great if you can solve it using recursion first and then explain as how this problem falls into DP category.

    • @soumavanag5025
      @soumavanag5025 7 ปีที่แล้ว +7

      u are right

    • @generalroboskel
      @generalroboskel 7 ปีที่แล้ว +16

      I agree that explaining the recursive or iterative brute force method first would be better. Doing so would help with making the connection from how you would solve the problem naively, to how you could use dynamic programming to solve it faster

    • @arfathali6669
      @arfathali6669 6 ปีที่แล้ว +4

      I dint find one video or good article, explaining recursion solution :(

    • @kanhar
      @kanhar 5 ปีที่แล้ว +10

      This is the recursive solution
      def li_seq(arr):
      if not arr:
      return 0
      if len(arr) == 1:
      return 1
      count = 0
      for i in range(1,len(arr)):
      end_at_i = li_seq(arr[:i])
      if arr[-1] > arr[i - 1]:
      count = max(end_at_i + 1, count)
      return count

    • @carty5448
      @carty5448 5 ปีที่แล้ว

      he wont do this cuz solving this problem by recursion it would take too much, just imagine that if u give a super long subseqvence it can take up to some months for your computer to find the solution, the best 1

  • @rohitparkhe689
    @rohitparkhe689 4 หลายเดือนก่อน

    This video is the definition of precise.

  • @aninsignificantman001
    @aninsignificantman001 2 ปีที่แล้ว

    Thanks a lot for explaining in the simplest way.

  • @gajju652
    @gajju652 6 ปีที่แล้ว +1

    first time I found best video of LIS question.
    thanks from bottom of my heart!

  • @jeremyedbert5092
    @jeremyedbert5092 3 ปีที่แล้ว

    Thank you so much for making this video easy to understand. Keep going on!

  • @uRamPlus
    @uRamPlus 4 ปีที่แล้ว +1

    Thank you, algoexpert explained it in 1 hour and it was still not clear as yours!! Cheers thnx👍

    • @aronpop1447
      @aronpop1447 4 ปีที่แล้ว

      lmao never buy algorithm courses

    • @ron0studios
      @ron0studios 3 ปีที่แล้ว

      just use geeksforgeeks or hackerrank, its wayy better and covers more stuff. Maybe not systems interview stuff but still

  • @ankitraj412
    @ankitraj412 4 ปีที่แล้ว

    Thank you, sir. We need people like you.

  • @notgiven3971
    @notgiven3971 2 ปีที่แล้ว

    thank you for this video this helped me in my upper div Data Structures class in university for a quiz.

  • @weijiang252
    @weijiang252 8 ปีที่แล้ว +14

    Thanks for your sharing ! If only I had seen it earlier 😭!

  • @harshalpriyadarshi
    @harshalpriyadarshi 9 ปีที่แล้ว +5

    Additional fact: To backtrack all you need to do is to maintain a separate array of size N, which stores the "j" for which the "i"th index was last updated. Then when you get the maxima in the original array, just search for the index of j recursively, until you reach a number which has the value of 1 in the original array

  • @RameshKumar-jc6qu
    @RameshKumar-jc6qu 6 ปีที่แล้ว

    Great, I was trying hard for 2 days to understand the algorithm by code. It became simple once I saw this video.

  • @swapanshaw4139
    @swapanshaw4139 9 ปีที่แล้ว

    All your videos are true awesome. Thank you a lot Tushar.

  • @anuveshkothari
    @anuveshkothari 9 ปีที่แล้ว

    your tutorials makes things quite easy..

  • @prakashhiranandani
    @prakashhiranandani 5 ปีที่แล้ว

    @Tushar You are awesome man , dp was a real pain for me, after seeing your videos, I am so comfortable, keep up the great work!!!!

  • @hrushikeshvasista4605
    @hrushikeshvasista4605 8 ปีที่แล้ว

    thank you for videos... I regularly refer you video tutorials, the explanation is really simple yet clear..!

  • @theblitz9
    @theblitz9 4 ปีที่แล้ว

    Awesome explanation!
    Far better than the one of GeeksForGeeks

  • @Thegreatindiaexpedition
    @Thegreatindiaexpedition 7 ปีที่แล้ว

    wao very good TUshar ... Simply Amazing

  • @ericren4909
    @ericren4909 9 ปีที่แล้ว

    Watched several your videos and subscribed. So helpful that I wish I found you earlier.

  • @sudharsanalaksmi2363
    @sudharsanalaksmi2363 6 ปีที่แล้ว +1

    Excellent videos!Great going..I haven't seen such wonderful videos with clear explanation.Only through your videos i came to know clearly what dynamic programming actually means. Still that i couldn't understand anything just i thought we need to build a 2D array by your video only i came t know how the principles of dynamic programming are satisfied.Keep posting videos.

  • @offchan
    @offchan 8 ปีที่แล้ว

    I like that you put the word Length at the end of LIS. It is stating that you completely understand it. Finding the length instead of the actual LIS itself was confusing me a lot!

  • @tejasvigupta07
    @tejasvigupta07 4 ปีที่แล้ว +2

    The recursive approach according to me:-
    1. Iterate through the array containing the sequence.
    2. We will have two options either we can select the first element or not.
    3. Now if the next element of the array is greater than the previous element we will again have two choices either we can select the element or not.

    • @kranjani3585
      @kranjani3585 ปีที่แล้ว

      no because its not guarenteed that next element itself would contribute to the ans

  • @xiaoxiang8340
    @xiaoxiang8340 8 ปีที่แล้ว

    Great! Better than all the verbose literal explanations all over the Internet

  • @mdzahidulislam4724
    @mdzahidulislam4724 7 ปีที่แล้ว

    Nice explanation, subscribed after watching first video of this channel, thanks

  • @rashmi_gaur
    @rashmi_gaur 2 ปีที่แล้ว

    Thanks Brother!!..Very Nice and clear explanation!!

  • @prshnt006
    @prshnt006 9 ปีที่แล้ว +4

    at 02:42, why would the value at -1 be 1 ? shouldn't it be 2 ? because the value represents the length of the LIS up until that point. so up until -1, LIS would be of length 2 containing 3 and 4.

    • @xiaomingchen7543
      @xiaomingchen7543 8 ปีที่แล้ว

      same question ... Anyone can explain why?

    • @wpp6986
      @wpp6986 7 ปีที่แล้ว +3

      It represents the longest increasing subsequence up until that point including the value in question. It's necessary because when you do the comparison later, you need to know the maximum value up to that point. If we set the value at -1 to 2, then a later 0 or 1 would give us a subsequence of 3, which isn't actually true because the full subsequence doesn't contain a -1. For example, if you walk through the algorithm like you described, we would have a longest subsequence value of 3 at value 0, and 4 at value 6.

    • @zhengmouduan9565
      @zhengmouduan9565 7 ปีที่แล้ว

      the index below shows `the LIS about the current number`, instead of `globally longest`, i.e. let's consider the number `6`, we have LIS: 3,4,6, when we check the 3, we add 1 to index of 6, when we check the 4, add 1 again, this process means that `we found at most 3 lengths increasing seq including 6 itself, after we launch this process for all the number, we can find the longest seq by checking the length index

    • @umangahuja1
      @umangahuja1 6 ปีที่แล้ว +1

      It represents Length of subsequence using elements till that index and *including* that element in the subsequence.
      So if we include -1 in subsequence, we get {-1} as subsequence.

  • @prateekgupta9364
    @prateekgupta9364 9 ปีที่แล้ว

    become fan of yours!! watched most of the videos , superb explanation !!

  • @dikshitrajkhowa
    @dikshitrajkhowa 8 ปีที่แล้ว

    Thanks tushar for explaining so nicely

  • @Profnegi
    @Profnegi 5 หลายเดือนก่อน

    bro thx a lot.. most of the youtuber now a days just focus on learning the dp and do recursive solution then convert into dp (tabulation) just follow the prewritten steps by some youtuber.. they don't understand, how amazing the tabulation is..

  • @kekehehedede
    @kekehehedede 8 ปีที่แล้ว

    Actually the best video among similar ones.

  • @konstantinshalnev2173
    @konstantinshalnev2173 9 ปีที่แล้ว

    Tushar, thank you! Very good explanation!

  • @127.0.0-k
    @127.0.0-k 7 ปีที่แล้ว

    great explanation Tushar

  • @nitinadarsh
    @nitinadarsh 3 ปีที่แล้ว +2

    this is the O(n^2) solution but in GFG is it mentioned to solve in O(nlogn).

  • @manishtripathy1549
    @manishtripathy1549 7 ปีที่แล้ว

    Awesome explanation... Thanks and keep up the great work

  • @amadousallah6634
    @amadousallah6634 9 ปีที่แล้ว

    Great job. Thanks for such a great explanation.

  • @coldstone87
    @coldstone87 2 ปีที่แล้ว

    Brilliant. Love you man.

  • @MessiLionel123
    @MessiLionel123 8 ปีที่แล้ว

    Excellent bro.. keep up the good work.

  • @debanshudas4260
    @debanshudas4260 8 ปีที่แล้ว

    Great work! Thanks a lot for sharing this!

  • @ShreyanshChordia
    @ShreyanshChordia 5 ปีที่แล้ว +1

    You are a great teacher, sir. Solved my doubt so easily.

  • @SmartProgramming
    @SmartProgramming 6 ปีที่แล้ว

    great sir, the way you have explained is awesome, keep it up sir 🙂🙂👍👍👍👍

  • @maoqiutong
    @maoqiutong 6 ปีที่แล้ว

    Excellent illustration and demonstration. It would be perfect if you can save candidate sub-sequences simultaneously.

  • @mikepuckett1913
    @mikepuckett1913 9 ปีที่แล้ว

    Fantastic explanation and video!

  • @manikaranjan1540
    @manikaranjan1540 7 ปีที่แล้ว +1

    Thankyou so much for these amaxing videos

  • @miltonkumar3655
    @miltonkumar3655 7 ปีที่แล้ว

    wow great explanation....very easy to catch

  • @arifashumbul
    @arifashumbul 5 ปีที่แล้ว

    awsome!!!!! Was stuck at this since long time seriously.. Thanks bro

  • @mystikcoder144
    @mystikcoder144 5 ปีที่แล้ว +10

    Really really great explanations, thank you very much. Just one suggestion : I thoroughly understood the solution but still I don't know the intuition behind coming up with this solution. Could you please explain that also? Oh And the time complexities as well. thanks again :) :)

    • @venkateshdec27
      @venkateshdec27 3 ปีที่แล้ว +5

      The intuition here is like an eye ball movement. Let's say you have a simple array [1,2,4]. The answer is 3. How ? In the eyeball movement you see the number 1 then register in your brain 1 . When you go to the next number 2, your brain takes the previous value from your brain which is 1 and add 1 more to it and store it back to 2 in your brain. Same thing when you move your eye to 4. Take the previous value from your brain which is 2 and add +1 which is the final answer (3). Here we lay down our brain as one dimension array. The default value is 1 that mean if you select any number randomly the answer is 1. E.g. [12,4]. If I choose 12 from my eye ball it is 1 or if I choose 4 also the default value is 1. The process of incrementing 1 from previous array value is same as reading the existing brain content and add 1. This is the thumb rule of knapsack problem. You keep updating your brain with the best increasing value by erasing the old content and update the new content. Same here we do select the maximum value of the current value or the previous value +1. Eventually any DP tabulation problem is just converting the eyeball movement calculation into array layout. Hope this helps you. Thank you!!!

  • @akshaypandita1838
    @akshaypandita1838 6 ปีที่แล้ว

    Thanks dude keep doing these kinda videos..

  • @msylvest55
    @msylvest55 9 ปีที่แล้ว +10

    Tushar, you are the shit. Couldn't get through DP w/o you.

    • @sahilattri51
      @sahilattri51 9 ปีที่แล้ว +2

      +Mike S. he's awesome!

  • @abdulazizkhan2124
    @abdulazizkhan2124 4 ปีที่แล้ว +1

    The of explaining is owsm ❤️

  • @chintalapativenkataramarahul
    @chintalapativenkataramarahul 5 ปีที่แล้ว

    Excellent solution! Thank you!

  • @o.bandura
    @o.bandura 8 ปีที่แล้ว

    Outstanding explanation!

  • @stanev123
    @stanev123 7 ปีที่แล้ว

    great explanation, keep up the work

  • @roshanmitra1117
    @roshanmitra1117 4 ปีที่แล้ว

    thank you so much Tushar Sir

  • @FANFILM123
    @FANFILM123 9 ปีที่แล้ว

    Good working example! Thank you.

  • @vishnuvardhan-cq7dr
    @vishnuvardhan-cq7dr 8 ปีที่แล้ว +19

    Dude you just killed it !! Awesome explanation !!

  • @jaideepbommidi8611
    @jaideepbommidi8611 9 ปีที่แล้ว

    excellent explanation.
    thank you very much

  • @jlecampana
    @jlecampana 5 ปีที่แล้ว +3

    I'm in Love with this Video and its Explanation.

  • @spicytuna08
    @spicytuna08 6 ปีที่แล้ว

    hi tushar. using 2D like longest increasing palindrome is much simpler. and this approach allows back tracking to print out the actual numbers.

  • @FILMSync
    @FILMSync 6 ปีที่แล้ว

    No better explanation exists! perfect!

  • @gideokkim7869
    @gideokkim7869 4 ปีที่แล้ว

    Thank you. It’s so helpful.

  • @prashantchauhan8788
    @prashantchauhan8788 4 ปีที่แล้ว

    This is awesome, thank you sir 😍

  • @kil0_387
    @kil0_387 2 ปีที่แล้ว

    great video, thank you!

  • @vivekkds287
    @vivekkds287 ปีที่แล้ว

    explanation couldnt get any better.

  • @malharjajoo7393
    @malharjajoo7393 8 ปีที่แล้ว

    Nicely explained !

  • @bharath_v
    @bharath_v 8 ปีที่แล้ว

    Thanks Tushar, for sharing this knowledge!

  • @faiqarif5765
    @faiqarif5765 4 ปีที่แล้ว +2

    Im out of words, i couldn't find a solution to this problem on my own

  • @Melloyallo3434
    @Melloyallo3434 8 ปีที่แล้ว

    Thanks a ton man ! Super helpful !

  • @uuhhsharma9744
    @uuhhsharma9744 5 ปีที่แล้ว

    very nice explanation

  • @victormillan2965
    @victormillan2965 9 ปีที่แล้ว +3

    What it is the best, worst and average case?

  • @VikasGupta2014
    @VikasGupta2014 8 ปีที่แล้ว +1

    I have one suggestion. Before solving any DP problem, please first solve it with recursion then only move to DP. This will help us in better understanding of the problem and why is the need of DP here.

  • @cartooncartel6493
    @cartooncartel6493 ปีที่แล้ว

    You are a legend.

  • @lusinehovsepyan6146
    @lusinehovsepyan6146 9 ปีที่แล้ว

    Thank you , good explanation.

  • @DeepakSaini7900
    @DeepakSaini7900 3 ปีที่แล้ว

    Gazab explanation

  • @ankitjain8184
    @ankitjain8184 8 ปีที่แล้ว

    very nice tushar :)

  • @jatinpathangi486
    @jatinpathangi486 8 ปีที่แล้ว +45

    Wouldn't your solution be O(n^2)?

    • @NovaMenteMedia
      @NovaMenteMedia 7 ปีที่แล้ว +5

      It is , he explains a O(n log n) in another video

    • @bryanbocao4906
      @bryanbocao4906 6 ปีที่แล้ว

      Could you tell me which video for O(n log n) ? Thanks!

    • @sarthakgupta072
      @sarthakgupta072 6 ปีที่แล้ว +5

      th-cam.com/video/S9oUiVYEq7E/w-d-xo.html

    • @ShreyanshChordia
      @ShreyanshChordia 5 ปีที่แล้ว

      yes

    • @Raj_Tanvar
      @Raj_Tanvar 5 ปีที่แล้ว

      Bhai DP laga le ab to ...concept to samajh aa hi gya na

  • @AbhishekKumar-yy3ui
    @AbhishekKumar-yy3ui 7 ปีที่แล้ว

    You're awesome. Keep up the good work :)

  • @SonuSonu-tk5pk
    @SonuSonu-tk5pk 7 ปีที่แล้ว

    sir ji great explanation

  • @jmpokar
    @jmpokar 9 ปีที่แล้ว

    Nicely Explained. Like it very much. In any of your videos, is it explained with O(N Logn N) complexity?

  • @abhik2927
    @abhik2927 7 ปีที่แล้ว

    It felt as though you came running and were out of breath when u started the video!

  • @yeah5037
    @yeah5037 2 ปีที่แล้ว

    very nice solution, thanks!

  • @abhinowporwal
    @abhinowporwal 8 หลายเดือนก่อน

    The OG DSA wala bhaiya

  • @lalitdudheria2580
    @lalitdudheria2580 9 ปีที่แล้ว

    Great explanation :)