Largest Divisible Subset | Dynamic programming | Leetcode

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.ย. 2024
  • 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.co...
    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...

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

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

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

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

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

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

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

  • @pendyalaabhishek8866
    @pendyalaabhishek8866 2 ปีที่แล้ว +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 .

  • @ahmedkhaled8046
    @ahmedkhaled8046 4 ปีที่แล้ว +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.

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

    16:53 the rap got me there

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

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

  • @ERROR-os9wm
    @ERROR-os9wm ปีที่แล้ว +1

    Best solution explained by any other video present in TH-cam....thanks a lot

  • @shalomalexander7811
    @shalomalexander7811 4 ปีที่แล้ว +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  4 ปีที่แล้ว

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

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

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

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

      Keep learning :)

  • @Subhasish8844
    @Subhasish8844 4 ปีที่แล้ว +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..

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

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

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

      Keep learning :)

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

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

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

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

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

    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  4 ปีที่แล้ว

      I solved and then realized it's LIS 😅

  • @100bands
    @100bands 4 ปีที่แล้ว +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  4 ปีที่แล้ว +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 4 ปีที่แล้ว

      @@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.

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

    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 ❤️

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

    Your solution works like a charm

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

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

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

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

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

    Clearest explanation! Appreciate what you’re doing.

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

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

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

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

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

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

  • @rohitsai806
    @rohitsai806 4 ปีที่แล้ว +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

  • @vishwajeetpawar3009
    @vishwajeetpawar3009 2 ปีที่แล้ว +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 !!!

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

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

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

    thanks bhaiya amazing teaching style

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

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

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

    Nicely Expalained Keep it up

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

    O bhai maza aa gaya

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

    nicely explained. Thanks! 👍

  • @siddhartha.saif25
    @siddhartha.saif25 3 ปีที่แล้ว +1

    thanks a lot! very nice explanation!

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

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

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

    amazing! crisp and clear explanation.

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

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

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

    Great explanation! Thank you very much

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

    great explanation, thanks for sharing 👍

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

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

  • @GARIMAJAIN-ww6mc
    @GARIMAJAIN-ww6mc 6 หลายเดือนก่อน

    Awesome explanation🔥🔥

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

    Good explanation 👍

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

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

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

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

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

      Thanks. Dunno java yet 😅

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

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

    • @100bands
      @100bands 4 ปีที่แล้ว +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;
      }
      }

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

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

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

    Luckily you are a genius and gladly an awesome teacher.

  • @yashgupta3127
    @yashgupta3127 4 ปีที่แล้ว +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  4 ปีที่แล้ว +1

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

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

    Thanks, Sir Ji:)

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

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

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

    Excellent as always 🙌

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

    you are simply amazing...!!!!

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

    easy explanation!

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

    Very well explained!

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

    Thanks a ton!!🙌🙌🙌

  • @legel93
    @legel93 4 ปีที่แล้ว +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  4 ปีที่แล้ว +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 4 ปีที่แล้ว

      @@techdose4u many thanks! :)

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

    Thanks man.

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

    best explanation

  • @divakarbhardwaj7322
    @divakarbhardwaj7322 4 ปีที่แล้ว +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  4 ปีที่แล้ว

      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 4 ปีที่แล้ว

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

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

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

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

      Follow LIS. It will have exactly same code.

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

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

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

      Later bro. Now I am busy with this.

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

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

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

    6:40 I suppose it is AND operation.

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

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

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

      @@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.

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

    excellent

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

    Good one, subbed!

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

    very smart,

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

    Thanks bhaiya 😊

  • @AnilGupta-iv1rz
    @AnilGupta-iv1rz 4 ปีที่แล้ว +1

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

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +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.

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

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

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

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

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

      By tommorow probably I will upload the process.

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

    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.

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

    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;
    }
    }

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

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

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

    Thanks

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

    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

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

    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  4 ปีที่แล้ว +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.

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

    Please provide the java code for all examples

  • @user-nw9fl1pe4p
    @user-nw9fl1pe4p 3 ปีที่แล้ว +1

    Which book can I learn DSA?

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

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

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

    Is there code for the brute force recursive solution?

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +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 4 ปีที่แล้ว

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

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

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

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

      Sure but it will take some time.

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

    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

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

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

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

    You are always awesome!

  • @RahulKumar-de7rn
    @RahulKumar-de7rn 4 ปีที่แล้ว

    Bro explain contest q4 q3

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

      Later bro. I am very with these daily problems.

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

    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

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

      it's OR not AND, one condition is true

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

    without sorting the answer will be wrong