DP 42. Printing Longest Increasing Subsequence | Tabulation | Algorithm

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 ก.ย. 2024
  • Lecture Notes/C++/Java Codes: takeuforward.o...
    Problem Link: bit.ly/3rVoIoq
    Please watch the video at 1.25x for a better experience.
    Pre-req for this Series: • Re 1. Introduction to ...
    a
    Make sure to join our telegram group for discussions: linktr.ee/take...
    Full Playlist: • Striver's Dynamic Prog...
    In this video, we solve the LIS DP using tabulation method, then we go on to print the LIS as well.
    If you have not yet checked our SDE sheet, you should definitely do it: takeuforward.o...
    You can also get in touch with me at my social handles: linktr.ee/take...

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

  • @krishnans1665
    @krishnans1665 2 ปีที่แล้ว +104

    Another approach which I found to be intuitive: We can store the elements of the array without duplicates in increasing order (Can be easily done with the help of TreeSet in java or set in cpp). Then again store these elements in a new array and find the LCS of the original array and the newly computed array. The LCS of these 2 arrays will be the LIS. For printing the LIS, we can use the same approach used for printing LCS.

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

      nice approach

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

      //Nice Approach -- JAVA Code
      import java.util.*;
      public class Main
      {
      public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      int n = sc.nextInt();
      int arr1[] = new int[n];
      TreeSet ts =new TreeSet();
      for(int i=0;idp[ind1-1][ind2]){
      ind2--;
      }else{
      ind1--;
      }
      }
      }
      //Reverse the LCS arrayList
      Collections.reverse(ans);
      System.out.println(ans);
      }
      }

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

      really intuitive,thanks

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

      better approach would be to just utilize the dp array for it , as we know the difference between the last element in our sequence and second last element is just 1 , so what we can do , we will start iterating over array from last --> 0, when ever we find dp[i] == max , we will add , or print , and then decrease our max to max--; so that no we try to find second last element and so on.
      for(int index = n - 1; index >= 0 ; index--){
      if ( dp[index] == max){
      System.out.print( arr[index] +" ");
      max--;
      }
      }

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

      But that will again be n2 solution. So doesn't help.

  • @studyonline3236
    @studyonline3236 ปีที่แล้ว +23

    6:03 The intuition behind this tabulation is Fibonacci (frog jump problem - similar to DP-04 of this playlist) but the condition could be like -
    Frog must jump in increasing fashion using maxing stones possible. In this regard,
    Plz spend 2 mins on this to get the intuition.
    (Input array can be used to simulate the frog-stone representation. EX - array = [2,5,1,7], the first stone is at a distance of 2 from the start, then the second stone is 5 units away from the previous stone etc)
    Frog(*), _____ = stones separated by a variable distance (based on the input array)
    __*__ **____** **____** __*__ ____ ____ __*__ (WITH 7 stones)
    "Frog can start from any of the stones"
    In the above example : frog jumped say a distance k from stone 1 to stone 4, then it cannot jump to another stone say 5 or 6 and it has only one stone left, which is >k distance -> stone 7 : So the number of stones it used are 3. Longest Increasing Sequence = 3.
    CASE - 2:
    stones distances = Input Array = [1, 2 ,5, 7, 15, 3], Ascending order
    Stone and frog representation:
    __1__ __2__ __5__ __7__ __15__ __3__
    Now the frog can use 1,2,5,7,15 - LISubseq = 5
    CASE - 3:
    stones distances = Input Array = [6,5,4,3,2,1], Descending order
    Stone and frog representation:
    (Stone 1 is already at a distance 6 from the start) __6__ __5__ __4__ __3__ __2__ __1__
    Intuitively, * cannot go to next stone from any of the starting stone, the LIS = 1 (num of stones used).
    A recap of the concept of DP-4 (a variation - frog jump in increasing fashion - inspired by Fibonacci),
    TC=O(n^2), SC=O(n) for dp and O(n) for recursion stack.
    #Recursion LOGIC
    The main intent of this comment is to let you guys know that you can use the Fibonacci logic(that you already know). You can think it of a fibonacci problem - Assume a frog can jump from any of the valid index(0

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

      Plz read the comment first.
      #code in python
      def f():
      dp=[1]*n
      for i in range(n):
      for k in range(i):
      if a[k]

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

      thanks brother

  • @karanprabhakar72
    @karanprabhakar72 2 ปีที่แล้ว +10

    Hi @take U forward this method can also be implemented with queue
    And one more thing, if the question changes to print all such subsets ( as in this case, you are printing only one ) , queue implementation will be handy
    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;
    int main() {
    int n = 10 ;
    vector arr = {10,22,9,33,21,50,41,60,80,1};

    vector dp(n,0);
    dp[0] = 1;
    int omax = 0;
    int index = -1;

    for(int i = 1 ; i < n ; i++) {
    int max = 0;
    for(int j = 0 ; j < i ; j++) {
    if(arr[i] > arr[j]) {
    if(max < dp[j]) {
    max = dp[j];
    }
    }
    }
    dp[i] = max+1;
    if(omax < dp[i]) {
    omax = dp[i];
    index = i;
    }
    }

    if(omax == 0) {
    omax += 1;
    index = 0;
    }

    cout

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

      Somethings are not right in this code.. instead of index==0...it should be dp[index]==1 to print lis...and d[pq]==value && nums[i]

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

      @@harpic949 hi Aditya if you run it in c++ , it will work. I == 0 means you have reached start of it and hence you can safely exit

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

      can you convert it into Tabulation aapne poori series mei aisi hi recursion likhwayi hai PLease REPLY ! 5 ghante se baitha hu shi answer nhi aa rha
      #include
      int fun(int i,int prev,int arr[],int n, vector &dp)
      {
      if(iarr[i])
      len=max(len,1+fun(i-1,i,arr,n,dp));
      return dp[i][prev]=len;
      }
      int longestIncreasingSubsequence(int arr[], int n)
      {
      // Write Your Code here
      vector dp(n,vector(n+1,-1));
      return fun(n-1,n,arr,n,dp);
      }

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

    This approach comes to work when we have said to print only one LIS for the array but if we want to all the LIS of the array then we need to use BFS kind of algorithm.

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

      #include
      int fun(int i,int prev,int arr[],int n, vector &dp)
      {
      if(iarr[i])
      len=max(len,1+fun(i-1,i,arr,n,dp));
      return dp[i][prev]=len;
      }
      int longestIncreasingSubsequence(int arr[], int n)
      {
      // Write Your Code here
      vector dp(n,vector(n+1,-1));
      return fun(n-1,n,arr,n,dp);
      }

    • @Area-fd8ht
      @Area-fd8ht ปีที่แล้ว

      ​@@ajaysaini5314bhai eska tabulation hai ??

  • @Zunan263
    @Zunan263 2 ปีที่แล้ว +20

    Dp on trees and graphs plz

  • @siddheshpawar2823
    @siddheshpawar2823 2 ปีที่แล้ว +35

    I think we don't have to use hash for printing sequence. We can start from an index in our array where we got maxi value and go in reverse manner. For every index we will check if our value in dp less by 1 as that of maxi then it will part of sequence we will reduce maxi by one for further finding the elements.

    • @aayush5474
      @aayush5474 2 ปีที่แล้ว +1

      that's what i thought too

    • @takeUforward
      @takeUforward  2 ปีที่แล้ว +17

      Yes, you can do that too, takes another N to backtrack if the length is smaller of lis.

    • @niteshverma8281
      @niteshverma8281 2 ปีที่แล้ว +1

      @@takeUforward
      # o(nlogn) solution
      int longestIncreasingSubsequence(int a[], int n)
      {
      vector dp;
      dp.push_back(a[0]);
      for(int i=1;idp.back()) dp.push_back(a[i]);
      else
      {
      int idx=lower_bound(dp.begin(),dp.end(),a[i])-dp.begin();
      dp[idx]=a[i];
      }
      }
      return dp.size();
      }

    • @Rk-tm8z
      @Rk-tm8z 2 ปีที่แล้ว +1

      same

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

      @@Rk-tm8z best

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

    Understood. Solved this with second approach just 2 days before. Great content!

  • @anuragC819
    @anuragC819 2 ปีที่แล้ว +59

    This is the only one that I have not understood properly till now. Till the memoization section it was fine but the tabulation, I did not exactly understand the inner loop

    • @NomanKhanK-IT-
      @NomanKhanK-IT- 2 ปีที่แล้ว

      me too bro

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

      inner loop is for prev as prev can be any index from (currInd - 1 ) to -1 (if their is no prev)

    • @harpic949
      @harpic949 2 ปีที่แล้ว +1

      @@deepaktiwari7059 pata h yaar itna to..par ye utna intutive nahi h

    • @altafmazhar7762
      @altafmazhar7762 2 ปีที่แล้ว +13

      @@harpic949 Bro tabulation intutive hota bhi nhi hai we just do it for auxillary space optimization, only recursive solution are inituitive

    • @pk4288
      @pk4288 2 ปีที่แล้ว +1

      ++

  • @jonu.1504
    @jonu.1504 9 หลายเดือนก่อน +4

    It can be solved in O(NlogN) using Patience Sorting algorithm

    • @abc-ym4zs
      @abc-ym4zs 8 หลายเดือนก่อน

      How to learn these many algorithms and improve our thinking it really very scary bro

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

    6:17 optimised tabulation , 17:40 -> print lis ,23:13 final code

    • @tushararora-uv1zp
      @tushararora-uv1zp 5 หลายเดือนก่อน +1

      Earlier ( in previous video) it was not allowed ( was not getting ac ) to have n^2 dp but now in tabulation it got accepted how ?

  • @stith_pragya
    @stith_pragya 8 หลายเดือนก่อน +1

    UNDERSTOOD......Thank You So Much for this wonderful video............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

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

    I cannot thank you enough for this lesson. Really appreciate for this step by step build ups

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

    understood .....this time it takes time but finally smjh aa hi gya...thanku striver

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

    Similar prob. : 1626. Best Team With No Conflicts

  • @Harshit126
    @Harshit126 2 ปีที่แล้ว +1

    Understood, respect! The last method could actually be used for Longest Ideal Subsequence also which has been giving TLE eternally :P
    Thanks a ton!

  • @muditkhanna8164
    @muditkhanna8164 ปีที่แล้ว +13

    the thing you did with the comparing dp[prev]+1

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

    Got the same question in my Capgemini test today. Thanks Bhaiya

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

      for which position?

    • @Area-fd8ht
      @Area-fd8ht ปีที่แล้ว

      Doggy style position ke liye😂😂

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

      thanks for sharing your experience👌

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

    A bit simpler and easier to understand code (without using the extra hash array) using the Tabulation method taught by striver:
    vector printingLongestIncreasingSubsequence(vector arr, int n) {
    //Step 1: Calculating length of one of the LIS
    vector dp(n,1);
    for(int ind=1;ind

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

    better approach would be to just utilize the dp array for it , as we know the difference between the last element in our sequence and second last element is just 1 , so what we can do , we will start iterating over array from last --> 0, when ever we find dp[i] == max , we will add , or print , and then decrease our max to max--; so that no we try to find second last element and so on.
    for(int index = n - 1; index >= 0 ; index--){
    if ( dp[index] == max){
    System.out.print( arr[index] +" ");
    max--;
    }
    }

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

      how can you be sure than wo isika part hai, there is also a possibility that us index tak ka uska LIS 2 hai but wo current index ka part hi nahi hai

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

      @@pratikshadhole6694 The elements in the subsequence does not matter, only the order must be maintained.

  • @ritikshandilya7075
    @ritikshandilya7075 2 หลายเดือนก่อน

    really really awesome approach , thanks for great learning Striver

  • @tanishkarawat5266
    @tanishkarawat5266 2 หลายเดือนก่อน

    Trying all given possibilites on comments, I think the code striver is teaching is best though its not intutive

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

    Understood Sir. Thank you so much.

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

    Its not the most optimal and ideal way to print, there is no need of hash table, you can iterate max_idx in this way. We can start from an index in our array where we got maxi value and go in reverse manner. For every index we will check if our value in dp less by 1 as that of maxi then it will part of sequence we will reduce maxi by one for further finding the elements.. Have a look at my code.
    int lengthOfLIS(vector& nums) {
    int n = nums.size();
    vector dp(n, 1);
    int maxi = 1;
    int max_idx = -1;
    for(int i = 0; i < n; i++){
    for(int prev = 0; prev< i; prev++){
    if(nums[prev] < nums[i]){
    dp[i] = max(dp[i], 1 + dp[prev]);
    }
    }
    if(dp[i] > maxi){
    maxi = dp[i];
    max_idx = i;
    }
    }
    while(max_idx >= 0){
    cout nums[idx] && dp[max_idx] == dp[idx] + 1) break;
    --idx;
    }
    max_idx = idx;
    }
    return maxi;
    }
    };
    Striver please pin this comment if you find it appropriate

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

    This is the Best DP series but, got to say that this question was the least intuitive one to solve.

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

    for next/curr to save space for this problem we only need one array since by the time we update the index it's already used

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

    that space optimization trick using next and prev is dope!

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

      bro I didnt get that can u tell which video is he refereing to

    • @lucygaming9726
      @lucygaming9726 9 หลายเดือนก่อน

      @@danishsharma496 watch the first video of dp on strings

  • @Ayush-lq3fz
    @Ayush-lq3fz 2 ปีที่แล้ว +2

    Hey striver. What is the timeline for the updation of notes. It's not there in the DP notes section.

  • @shreyatiwari8239
    @shreyatiwari8239 2 หลายเดือนก่อน

    can we directly use this tabulation approach in interviews for prev lis question

  • @d_0364
    @d_0364 2 หลายเดือนก่อน

    I think we can do it in NlogN + PlogP where P is LIS size

  • @devbhattdev1607
    @devbhattdev1607 2 ปีที่แล้ว +1

    please provide documentation or write ups of the this algorithms please

  • @RitikKumar-lv8cm
    @RitikKumar-lv8cm 2 ปีที่แล้ว +1

    bhaiya confuse about co-ordinate change please clear doubt

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

    why is it that if we apply increasing for loop that is 0 to n for index and -1 to index-1 for previous index rather than the decreasing way mentioned in the video the answer is coming wrong, it would be really helpful if anyone could explain💡

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

      I also have the same doubt, not able to understand how Striver changed it from recursion to tabulation

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

      Hi Hardik, you cannot apply index from 0->n because while calculating dp[i] it is dependent on dp[i+1] so if you do it from 0->n intilally all will zero ahead of that index and you will get wrong answer. But while calculating it from n->0 the ahead one will be already calculated and add to the answer. Hope this helps

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

      bro its bottom up nah so we do it in reverse way only, for that you shd make recursive equation from the n-1 idx and ques will be longest decreasing sequence for that to implement

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

    why are second parameter going in +1 states?

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

    the best thing about striver vaiyya is he normalize unsuccessful submission, i mean i used to think coder like them get successful submission at the first time itself and are never wrong , now i dont get panic when i see LTS or error or anything , it feels like okk lets try again :)

  • @paneercheeseparatha
    @paneercheeseparatha 25 วันที่ผ่านมา +1

    Didn't completely get that state shift approach. Could appreciate more problems involving that. Can you or someone pls suggest any related problems?

    • @ugthesep5706
      @ugthesep5706 18 วันที่ผ่านมา

      Please watch previous videos he explained we did shift so that it can work with dp as dp can't contain a negative index that is why we did +1 to make -1 prev_ind start from 0

    • @paneercheeseparatha
      @paneercheeseparatha 18 วันที่ผ่านมา

      @@ugthesep5706 oh ok. thanks!

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

    printing O(N)
    // printing lis
    int temp = omax;
    String lis = "";
    for(int i = dp.length-1;i>=0 && omax!=0;i--){
    if(dp[i] == omax){
    lis = nums[i] +" "+lis;
    omax-- }
    }

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

    understood the initial tabulation , Thanks

  • @lakshaysharma6550
    @lakshaysharma6550 2 ปีที่แล้ว +1

    UNDERSTOOD!!!!🔥🔥🔥🔥

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

    Last 4 mins ,.. Striver on God Mode

  • @coolgaurav5163
    @coolgaurav5163 2 หลายเดือนก่อน

    UNDERSTOOD

  • @ShlokMansata
    @ShlokMansata 2 หลายเดือนก่อน

    Why we cannot start from beginning like we used to do in other problems

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

    Understood !!

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

    Thanks Striver. Understood.

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

    Understood!

  • @vaibhavjaiswal5911
    @vaibhavjaiswal5911 13 วันที่ผ่านมา

    Why did you all of a sudden reverse the iteration for memoization and tabulation
    In Previous videos in memoization u went from n-1 -> 0, but here you reversed it
    And this is very confusing,

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

    Really definition of previous index is previous index, I think you could have explain that part in a better way !!1

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

    Understood

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

    we have used N*N sized array in tabulation also then why it is giving error in memoization only ?

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

    second parameter was not always stored in +1 state in memoization as you stated in this video at 3.35. Why have you taken the index+1 as second parameter whereas in the memoization it was just index by updatiing about its current index which is actually greater than the previous index's value.

    • @amanmishra-vt8hk
      @amanmishra-vt8hk 7 หลายเดือนก่อน

      Because we are converting prevIndex whose range is between (n-1 to -1) to (n to 0) so, for every value that denotes prevIndex should be incremented by +1,that's why even when we are returning the answer instead of returning of [0][-1] we are return [0][-1+1].

  • @sourabhgarg2890
    @sourabhgarg2890 6 หลายเดือนก่อน

    not understood why index+1 at 3.40

  • @immortal6978
    @immortal6978 10 หลายเดือนก่อน

    Really challanging for me to make it today >>>

  • @ramakrishnakcr4417
    @ramakrishnakcr4417 6 หลายเดือนก่อน

    understood

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

    Striver aka Raj brother one small correction @ 6:54
    dp[i] = signifies the longest LIS that ends at i.
    Correct is dp[i] = signifies LIS that starts at i
    Bcoz ur Recursion call was f(0,-1) 0 to N. Thereby Tabulation code is N to 0.
    So dp[i] is when LIS is starting from ith index till End (N-1).
    I hope i am correct. Please someone let me know. Also like and support if it's correct.
    But for the Next New Printing Code
    Dp[i] = signifies the longest LIS that ends at i.

    • @TanishkPatil-z7u
      @TanishkPatil-z7u ปีที่แล้ว

      im not sure but his recurisive calls for tabulation approach are from n->0 hence tabulation from 0->n-1 , so dp[i] ends at i is correct imo

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

    thankyou so so much sir

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

    Thankyou @striver

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

    I face problem while converting memo solution to tabulation solution how to overcome that?

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

    thanks

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

    Understood !! ❤

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

    Understood!!Thank you

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

    is printing LIS asked in interviews I am planning on leaving this

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

    Thanks for that 👍

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

    why are we adding +1 to prev if we're using range -1 to ind-1 in tabulation?

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

      because prev's initial value is -1 which is not a valid array index.

    • @amanmishra-vt8hk
      @amanmishra-vt8hk 7 หลายเดือนก่อน

      Because we are converting prevIndex whose range is between (n-1 to -1) to (n to 0) so, for every value that denotes prevIndex should be incremented by +1,that's why even when we are returning the answer instead of returning of [0][-1] we are return [0][-1+1] @aditi1729 .

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

    Bhaiya can u teach how to do dfs with memoization?

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

      Dfs already uses stack

    • @cse-b-132ashishupadhyaya5
      @cse-b-132ashishupadhyaya5 2 ปีที่แล้ว

      DFS has no overlaping Subsequence ,so no point of using Memoization there!!!!!!

  • @divyanshugupta5006
    @divyanshugupta5006 11 หลายเดือนก่อน +1

    🔥

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

    can you provide the handwritten notes of dp series??

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

    when i am checking (1+dp[j]>dp[i]) inside if then it is giving TLE but if i include this condition in if then it is passing all TCs. Can you explain plz?

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

    Why do u want to create another array i.e. hash and make the things complicated ?
    My Approach :
    vectordp(n,1);
    for(int i=0;i

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

      it is perfect!!

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

      But it fails @gfg where question ask to returns the Longest Increasing subsequence which is lexicographically smallest corresponding to the indices of the elements
      Overall nice 🙂

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

    understood

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

    but what is there are more than one LISs.

  • @sahilgagan2242
    @sahilgagan2242 2 ปีที่แล้ว +1

    at 4:46 i write the same code but runtime error someone help pls

  • @LowkeyCoder
    @LowkeyCoder 9 หลายเดือนก่อน

    "Understood"!

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

    we can't find notes for these videos , and I know java only. Please provide with the source code also.PLease

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

    Understood

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

    Understood,
    I think we can also do without hash array
    Where ever we find maxi we can store in ans
    And in every loop keeping value in some curr array

  • @aarifhannan4429
    @aarifhannan4429 2 ปีที่แล้ว +1

    Most Efficient Code:-
    #include
    int longestIncreasingSubsequence(int arr[], int n){
    vector lis;
    lis.push_back(arr[0]);
    for(int i=1;i

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

    When prev_ind == -1 then arr[ind] > arr[prev_ind] why this is not giving runtime error?

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

      becoz of shortcircuit in logical or operator(if first statement is true then next statement will not execute)

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

    Striver could you please upload the code and the notes for the videos from 38.

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

    why a dp array of N+1 x N + 1 ??

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

      you can make the dp array of dp[N][N];
      but then your base case will gets changed
      base case --> new
      if(ind == nums.length-1){
      if(prev == 0 || nums[ind]>nums[prev-1]) return 1;
      return 0 ;}

  • @Galactushere
    @Galactushere 2 หลายเดือนก่อน

    Another approach
    vector longestIncreasingSubsequence(int n, vector& arr) {
    // Code here
    vector dp (n+1, 1);
    for(int idx = 0; idx

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

    understood!!

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

    understood✨

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

    Understood !

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

    understood sir🙏🙏

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

    Us

  • @siddharth794
    @siddharth794 2 ปีที่แล้ว +1

    Not understood, last mai kaafi tezii se kuch bhi krdia

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

    Understood bhaiya !!!

  • @abhijeetmishra3804
    @abhijeetmishra3804 7 หลายเดือนก่อน

    ye code to khatam hi nhi ho rha 😅😅😂😂😂😂 ... Striver G.O.A.T

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

    Understood ❤

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

    22:00

  • @aryankumar3018
    @aryankumar3018 27 วันที่ผ่านมา

    Unsderstood

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

    What if I do this after filling dp[]?
    idx; // get it while filling dp, its idx at which we get max length
    mx; // stores the max Length
    vector v;
    v.push_back(a[idx]);
    for(int i=idx-1; i>=0; i--)
    {
    if(a[i]

  • @ShubhamKumar-fn5be
    @ShubhamKumar-fn5be ปีที่แล้ว

    understood😄😄

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

    US

  • @CiaranFarley-mn1zl
    @CiaranFarley-mn1zl 3 หลายเดือนก่อน

    US

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

    Understood !!1

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

    Tough question bap re

  • @divyamkumar9558
    @divyamkumar9558 2 ปีที่แล้ว +1

    this was a little too fast..u should have taken little more time to explain properly how lis is printed

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

    mai padhte/revise karte karte thak gaya lekin yeh banda padhate hue nahi thaka!!

  • @051-avnee4
    @051-avnee4 ปีที่แล้ว

    US :)

  • @Parthj426
    @Parthj426 2 หลายเดือนก่อน

    us