Largest Divisible Subset | Dynamic programming | Leetcode

Sdílet
Vložit
  • čas přidán 12. 06. 2020
  • This video explains a very important dynamic programming problem which is a variant of longest increasing subsequence (LIS).The problem is to find the largest divisible subset in such a way that all the elements in the subset are pairwise divisible.We need to return all the subset elements irrespective of their order.I have explained how to apply recursion and backtracking to this problem and i have also shown why recursion will fail.I have shown how this problem can be reduced to longest increasing subsequence problem (LIS).I have shown all the steps clearly with proper examples and at the end, i have shown the code walk through.CODE LINK is present below as usual.For the DAY-13 leetcode problem "Largest Divisible Subset", SORTING is essential and it cannot be solved without sorting because a number should have all the divisors to its left to be absolutely sure about including it in the same subset.This ASC order is important for decision making. Consider [9,18,6]. Here, if we are at 6 then we will compare with 18 and it will divide and so we will try to include in the SET with 18. But 18 is already in a SET with 9 which is [9,18] and therefore this will give wrong answer. If we first sort it to [6,9,18], then this will always give you correct answer.So, please ignore what i said about solving without sorting. 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 :)
    =================================================================
    INSTAGRAM: / surya.pratap.k
    LinkedIn: / surya-pratap-kahar-47b...
    =================================================================
    CODE LINK: gist.github.com/SuryaPratapK/...
    SIMILAR PROBLEMs:-
    Longest Increasing Subsequence (LIS): • Longest increasing sub...
    Longest common subsequence (LCS): • Longest common subsequ...
    Longest common substring: • Longest common substri...
    Longest palindromic substring (LPS): • Longest palindromic su...

Komentáře • 152

  • @jamebozo
    @jamebozo Před 4 lety +30

    by far the clearest explanation i've come across, thanks for explaining the thought process! you're awesome.

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

    what makes your different is you explain the thought process which includes the complexity stuff in detail. Absolutely brilliant

  • @lampyclampy
    @lampyclampy Před 4 lety +10

    keep posting , this is the best way to teach DS and ALGO by solving problems .

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

    Very good. You explained everything in a fast and simple way. The intuitive, the idea, the code... everything. Thank you and keep up the good work.

  • @devendradahale546
    @devendradahale546 Před 3 lety

    Great approach . When I saw this problem I wasn't able to come up with identifying some overlapping subproblems. You explained it very clearly as it is variation of lis and the transitivity property by which we do it by dp. Thank you keep doing great work and keep helping others ❤️

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

    Thanks for posting the June Leetcode Challenge problems. Your explanation is really helpful!!!

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

    I saw many videos...but this video just made the whole dp[] table construction concept very clear. I Salute you!!!!. Teach me how you learnt to solve like this

    • @techdose4u
      @techdose4u  Před 4 lety

      Thanks bro. Keep learning and practicing. You will master is soon :)

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

    you deserve a lot more subscribers . only thing is if people don't even know the basics and watch this video they wont like it .i am asking people to not forget liking this video .

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

    Thank you TECH DOSE. As a beginner, i have learnt so many concepts from this channel.Thank you for existing. :3

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

    I feel fortunate enough to have found your channel:)
    Thanks a lot !

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

    I dp was a list of list..but the way you found the whole max list by just storing the length was awsome..Generally what I do is I solve the daily challenge and then I come back and see how you have done it...and I am surprised everytime..keep up the good work..

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

    You are doing such a good job of posting these videos. Thanks a ton for such wonderful explanations!

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

    Amazing explanation. I read the solution on LC but didn't really get it. this video is a lifesaver!

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

    Awesome explanation. No doubt this needs to be the best solution video for this problem on the internet.

  • @100bands
    @100bands Před 4 lety +1

    Always on point. It works without the previous variable as well. You just need to decrement max whenever you find a match

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

      Let's say your array is (1,2,3,4). Now your DP table will be (1,2,2,3). Your max will be 3 and you start with last element 4. 4 will get included, max will be reduced to 2. Now 2 matched the 3rd element and so 3 will also get included which is wrong because we need to check if it divides the previous element and so we need to store the previous element somewhere. So how did you manage this?

    • @100bands
      @100bands Před 4 lety

      @@techdose4u Ah I see, you are right.. I guess the test case is incomplete because mine actually got accepted without checking the divisibility of previous element.

  • @ShubhamMahawar_Dancer_Actor

    Initially ,i tried by myself but failed to get any approach then just saw your video and by the time you said LIS, i solved it ny myself and got accepted.
    But how you have thought is it LIS variant problem,main your thinking ability damn good.

    • @techdose4u
      @techdose4u  Před 4 lety

      I solved and then realized it's LIS 😅

  • @ERROR-os9wm
    @ERROR-os9wm Před rokem +1

    Best solution explained by any other video present in CZcams....thanks a lot

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

    Even though I could solve the question, I find it really useful to see your videos

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

    Small mistake from 8:35 the condition should be arr[i] % arr[j] == 0. But overall great content, please keep posting videos.

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

    thanks for posting made concept very clear no waste of time whole video is content learnt so much

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

    I stored the ans in a vector every time the max is updated. But the way you found out the subset finally was awesome

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

    Sir you are awesome and the way you explain problem makes my day. I really appreciate your effort and time 👏👏

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

    Thanks for a Great Explanation. And Special Thanks for doing it in C++.

  • @noctua7771
    @noctua7771 Před 5 měsíci

    Great explanation! Thank you very much

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

    Really nice explanation , I was confused about how this problem is related to LIS and you cleared my doubt😇😇😇 !!! thanks a lot , very very clear explanation !!!

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

    Now whenever I search questions on youtube, I always hope u uploaded the ques.

  • @paragroy5359
    @paragroy5359 Před 2 lety

    Nice explanation your videos are really good...please keep on making such videos...you are doing a great job.

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

    Thank you very much for your effort! It was really helpful and clear!

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

    Your solution works like a charm

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

    amazing! crisp and clear explanation.

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

    I love your videos, really a good way to improve sills in DS algo

  • @The_AK31
    @The_AK31 Před 3 měsíci +1

    16:53 the rap got me there

  • @siddhartha.saif25
    @siddhartha.saif25 Před 3 lety +1

    thanks a lot! very nice explanation!

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

    wow ... finding it as a variant of LIS is just cool.

  • @downtowngedi
    @downtowngedi Před 2 lety

    great explanation, thanks for sharing 👍

  • @srikantsharma6430
    @srikantsharma6430 Před 4 lety

    Very well explained!

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

    nicely explained. Thanks! 👍

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

    Thank you sir 👍 I daily watch your awesome video sir .♥️

  • @akshatsinghbodh3067
    @akshatsinghbodh3067 Před 2 lety

    Thanks a ton!!🙌🙌🙌

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

    you are simply amazing...!!!!

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

    Urghhh i was so close to the solution.I am new to programming and have not touched dp yet but unknowingly i was on right track but was having problem with how to return the elements the part where you used prev idk why it didnt came to my mind.

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

      :) It's good to see that even without DP experience you were close.

  • @GARIMAJAIN-ww6mc
    @GARIMAJAIN-ww6mc Před 5 měsíci

    Awesome explanation🔥🔥

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

    Excellent as always 🙌

  • @hymnish_you
    @hymnish_you Před 2 lety

    Luckily you are a genius and gladly an awesome teacher.

  • @akashyadav3211
    @akashyadav3211 Před rokem

    Good explanation 👍

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

    Hi, the explanation really helps, thanks!
    At the end of your video the code suggests you do dp[i] = dp[j] + 1 isn't it supposed to be dp[i] ++? This when the conditions are met and you increment dp[i]
    Thanks for any replies in advance!

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

      That might work because we are going from left to right and checking sequentially band the elements can only increase by one value maximum moving to the right.

    • @legel93
      @legel93 Před 4 lety

      @@techdose4u many thanks! :)

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

    Is there any approach in recognising these dp problems? Like you recognised Longest Increasing Subsequence. Is there any method other than just trial and error to approach a problem like this?

    • @techdose4u
      @techdose4u  Před 4 lety

      The difficult part in this problem was to recognise it's a LIS problem. I solved it then realized it's LIS 😅 There is no said rule of identification. You will just recognise it easily if you have practiced a lot.

    • @divakarbhardwaj7322
      @divakarbhardwaj7322 Před 4 lety

      @@techdose4u alright. Practice is the only answer then.😅

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

    How can we find the answer (the subset) if we have taken the recursive/memoization approach (top-down) ?

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

      Follow LIS. It will have exactly same code.

  • @rururi3262
    @rururi3262 Před rokem

    easy explanation!

  • @competitiveprogrammingwith7752

    Nicely Expalained Keep it up

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

    thanks bhaiya amazing teaching style

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

    Thanks, Sir Ji:)

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

    Hi, thanks for the clear explanation. Can you please explain the Rain Trapping Water II from leetcode?

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

      Later bro. Now I am busy with this.

    • @ALOKRAWSINGHAM
      @ALOKRAWSINGHAM Před 4 lety

      Actually I had faced that question recently in one of the company but I was not able to solve.

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

    Bro write the java code also ...btw your explanation is simply superb ♥️

    • @techdose4u
      @techdose4u  Před 4 lety

      Thanks. Dunno java yet 😅

    • @exodus5948
      @exodus5948 Před 4 lety

      @@techdose4u maybe all companies use java python , but cpp has separate fan base

    • @100bands
      @100bands Před 4 lety +2

      Java Code:
      class Solution {
      public List largestDivisibleSubset(int[] nums) {
      Arrays.sort(nums);
      List subsets = new ArrayList();
      if(nums.length == 1){
      subsets.add(nums[0]);
      return subsets;
      }
      int[] dp = new int[nums.length];
      Arrays.fill(dp, 1);
      int max = 0;
      for(int i = 1; i < nums.length; i++){
      for(int j = 0; j < i; j++){
      if(nums[i] % nums[j] == 0 && 1 + dp[j] > dp[i]){
      dp[i] = 1 + dp[j];
      }
      max = Math.max(max, dp[i]);
      }
      }
      for(int k = dp.length-1; k >= 0; k--){
      if(dp[k] == max){
      subsets.add(nums[k]);
      max--;
      }
      }
      return subsets;
      }
      }

  • @ShubhamMahawar_Dancer_Actor

    Can you provide pseudo code for ,if we don't SORT the array how it will done cz i am getting WA while this approach

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

      I have provided the answer in community post. We can't solve this without sorting.I have explained why. Please go through the post.

  • @taxila4047
    @taxila4047 Před 5 měsíci

    The explanation is very clear .But why dp size is n+1.

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

    Good one, subbed!

  • @ayushgarg5929
    @ayushgarg5929 Před 3 lety

    How to get original subset...the subset you have found is sorter subset ??

  • @AnilGupta-iv1rz
    @AnilGupta-iv1rz Před 4 lety +1

    Is it really needed to maintain previous element while constructing subset?

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

      I did it to see if the Max value matches then the current element should also divide the larger (prev) element. So I maintained it. People have also solved without prev.

  • @priyanshuchaudhary1135

    best explanation

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

    what about article writing...many of us want to contribute articles on your website....

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

      By tommorow probably I will upload the process.

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

    O bhai maza aa gaya

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

    Thanks man.

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

    Is there code for the brute force recursive solution?

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

      I don't have it with me. Try writing it. It will be simple. Very similar to LIS recursive code. You need to just add your division constraint to that.

    • @victorcui4014
      @victorcui4014 Před 4 lety

      @@techdose4u Thanks I wrote something like pastebin.com/E2vUjP0L in Python

  • @hhcdghjjgsdrt235
    @hhcdghjjgsdrt235 Před 5 měsíci

    very smart,

  • @starc701
    @starc701 Před 3 lety

    16:06 , i dont understood how (6,3,1) is a subset ? is it not a subsequence ?
    I think the question statement is wrong , how can leetcode write subset , question should be find the largest subsequence.

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

    excellent

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

    6:40 I suppose it is AND operation.

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

      No it is OR operation. Please read the question again.

    • @ijaz2020
      @ijaz2020 Před 4 lety

      @@techdose4u Any two numbers you can take (a%b == 0 || b%a == 0) this condition will always be true. We will end up adding every element to the subset.

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

    Thanks bhaiya 😊

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

    Hey buddy, could you please create video about z algorithm?

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

      Sure but it will take some time.

  • @tanyakemkar310
    @tanyakemkar310 Před rokem

    Thanks

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

    OH MY GOD! I could never have linked it to LIS. Now I feel so stupid =.=``

  • @shivannagarivenkatreddy7623

    Please provide the java code for all examples

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

    You are always awesome!

  • @user-nw9fl1pe4p
    @user-nw9fl1pe4p Před 3 lety +1

    Which book can I learn DSA?

    • @techdose4u
      @techdose4u  Před 3 lety

      Can't recommend you any DSA book since I din't follow anything.

  • @sudhanshusingh211
    @sudhanshusingh211 Před 3 lety

    why the order is not important subset is part of the array by removing some element by maintaining order. How 1,2,4 is subset of 1,4,2,6,7

  • @siddharthbora3961
    @siddharthbora3961 Před 2 lety

    napa valle
    class Solution {
    public:
    vector largestDivisibleSubset(vector& n)
    {
    sort(n.begin(),n.end());
    vectorhawa(n.size(),1);
    vectorhawa2(n.size());
    hawa2.push_back({n[0]});
    for(int i=0;i

  • @sanketdedhia9981
    @sanketdedhia9981 Před 4 lety

    Java Code with the same logic:
    class Solution {
    public List largestDivisibleSubset(int[] nums) {
    List result = new ArrayList();
    int n = nums.length;
    if(n == 0) return result;
    Arrays.sort(nums);
    int max = 1;
    int[] dp = new int[n+1];
    dp[0] = 1;
    for(int i = 1; i=0; i--){
    if(dp[i] == max && (prev%nums[i] == 0 || prev == -1)){
    result.add(nums[i]);
    max-=1;
    prev = nums[i];
    }
    }
    return result;
    }
    }

  • @RahulKumar-de7rn
    @RahulKumar-de7rn Před 4 lety

    Bro explain contest q4 q3

    • @techdose4u
      @techdose4u  Před 4 lety

      Later bro. I am very with these daily problems.

  • @abhinavranjan3599
    @abhinavranjan3599 Před 2 lety

    isn't the question wrong?
    It should be subsequence and not subset.
    A subset cannot have interleaving elements but a subsequence can.

  • @vamshikrishna8143
    @vamshikrishna8143 Před rokem

    Can someone tell me how this is true?
    answer[i] % answer[j] == 0, or
    answer[j] % answer[i] == 0
    since
    1 % 2 = 1 and
    2 % 1 = 0

    • @rururi3262
      @rururi3262 Před rokem

      it's OR not AND, one condition is true

  • @shivamjoshi6648
    @shivamjoshi6648 Před 3 lety

    without sorting the answer will be wrong

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

    Clearest explanation! Appreciate what you’re doing.