Partition into Subsets Dynamic Programming | Explanation and Code

Sdílet
Vložit
  • čas přidán 6. 08. 2020
  • Please consume this content on nados.pepcoding.com for a richer experience. It is necessary to solve the questions while watching videos, nados.pepcoding.com enables that.
    NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. In this video, we discuss the question Partition into Subsets where we are required to find the number of ways in which n elements can be divided into k subsets such that no subsets is empty. In this problem,
    1. You are given a number n, representing the number of elements.
    2. You are given a number k, representing the number of subsets.
    3. You are required to print the number of ways in which these elements can be partitioned in k non-empty subsets.
    E.g.
    For n = 4 and k = 3 total ways is 6
    12-3-4
    1-23-4
    13-2-4
    14-2-3
    1-24-3
    1-2-34
    To attempt and submit this question, click here: www.pepcoding.com/resources/o...
    For a better experience and more exercises, VISIT: www.pepcoding.com/resources/o...
    #dp #dynamicprogramming
    Have a look at our result: www.pepcoding.com/placements
    Follow us on our FB page: / pepcoding
    Follow us on Instagram: / pepcoding
    Follow us on LinkedIn: / pepcoding-education

Komentáře • 89

  • @motivationalvideo3702
    @motivationalvideo3702 Před 2 lety +11

    Last year at this i didn't even had the laptop. I managed to purchase one in septempber, at that time i had zero knowledge of programming and now i can solve problems on my own.This is all possible because of you.
    Thanks a lot sir for providing this amazing content for free.🙂🙂🙂😊😊

  • @GopalKumar-py2cf
    @GopalKumar-py2cf Před rokem

    well explained

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

    For such videos I hope CZcams gives a love react . Like doesn't justify the love for your videos sir.

    • @Pepcoding
      @Pepcoding  Před 4 lety +11

      Thanks for liking the content, I request you could write about our channel on your facebook or linkedin? So that maximum students could get the best out of it.

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

      @@Pepcoding surely sir. Thanks for your hard work. This content helps people who can't afford handsome fees but need to learn DSA.
      I WILL SURELY SHARE IT. THABKS AGAIN

  • @AshwaniSharma-zw6wq
    @AshwaniSharma-zw6wq Před 3 lety +17

    17: 05 dp[4][5]=4*1+6=10. Beside that as usual Awesome explanation sir🔥

  • @shirshendunayek2470
    @shirshendunayek2470 Před rokem

    you are too good

  • @suyashgupta1741
    @suyashgupta1741 Před 3 lety +36

    22:25 Even after explaining soo well you did it all again. This really shows how passionate you are! Thank you sir!

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

    Sir aapse behtar shayad ye problem koi aur nahi samjha pata. Thank you, and get well soon.

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

      Thank you. Bro, linkedin ya fb pe ek post daal do channel ki. Bacho ko pta chal jaega.

    • @rajattalnikar6167
      @rajattalnikar6167 Před 4 lety

      @@Pepcoding Sure sir mai apne sabhi juniors ko advice krunga ki wo aapko follow kre.

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

    The best channel. The best explanation and variety.

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

      Thank you. May I request you to write about us on linkedin?

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

      @@Pepcoding I am following you on LinkedIn from my different account.

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

    Sir your teaching skill is awesome...

  • @Mandeepsingh-jo5cf
    @Mandeepsingh-jo5cf Před 3 lety +1

    Thank you Sir.

    • @Pepcoding
      @Pepcoding  Před 3 lety

      Thankyou beta!
      I am glad you liked it. I hope that you are watching till the end and trying to understand what, how, and especially why of the problem.
      If you like our efforts, will you like to write a few words about us here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )

  • @sonalisaxena22
    @sonalisaxena22 Před 3 lety

    Thank you sir,

  • @AshishGusain17
    @AshishGusain17 Před 2 lety

    thanks for these videos

  • @shankysays
    @shankysays Před 2 lety

    This is good question. Had to think hard to visualise everything.

  • @syamsreemanojreddy1027

    hatsoff sir.. you are a legend.

    • @Pepcoding
      @Pepcoding  Před 2 lety

      Hope you love the explanation, for better experience and well organised content sign up on nados.io and start learning.

  • @democrats9579
    @democrats9579 Před 2 lety

    I love Pepcoding!!

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

    amazing : )

  • @AmanRaj-nn6xx
    @AmanRaj-nn6xx Před 2 lety

    hats off to your dedication sir!

    • @Pepcoding
      @Pepcoding  Před 2 lety

      Hope you like the video but if you want to excel your coding career with better experience and well organised content.
      visit - nados.pepcoding.com
      Don't forget to follow us on Instagram instagram.com/pepcoding/

  • @tarunkumar3279
    @tarunkumar3279 Před rokem +1

    sir how will we get intution of getting 2d dp

  • @tusharsinghal1429
    @tusharsinghal1429 Před 2 lety

    Sir, I am a big fan of your explanation 🤘

  • @akshatgupta2607
    @akshatgupta2607 Před 2 lety

    GAWD level content ❤️

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

    was waiting eagerly for your videos

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

      I hope I did justice with this one. Was feeling very weak.

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

      Hope you like it. If yes, I request you to write about our channel on your facebook or linkedin? So that maximum students could get the best out of it.

    • @kushagrashekhawat8227
      @kushagrashekhawat8227 Před 3 lety

      @@Pepcoding Tabhi sir bahut aacha samjhaya aapne. Jaise formula aapne derive kara usse kaafi aacha thought process develop hua

  • @rishabhgoyal8110
    @rishabhgoyal8110 Před 2 lety

    God bless him!

    • @Pepcoding
      @Pepcoding  Před 2 lety

      For more such content, do check out nados.pepcoding.com

  • @sakshamsrivastava6280
    @sakshamsrivastava6280 Před 3 lety

    just wow

  • @maskedrandom8734
    @maskedrandom8734 Před rokem +1

    Can someone in the audience or @Pepcoding explain why are we considering to split the problem in two ways - eg (1234 in 3 teams) - 123(k) and 123(k-1) specifically. How did we get down to this?

    • @rafikulalam59
      @rafikulalam59 Před rokem

      lets say 1234(3)
      so 4 will ask '123'
      1. hey '123' an you divide yourselves into 3 team? 123(3)
      if you guys can then i will join any one of the team
      2. hey '123' can you divide yourself into 2 team? 123(2)
      if you guys can then i will make a seperate team.
      these two can be the possible scenarios
      why 4 will not ask '123' to divide in 1 team? 123(1)?
      because if it says so
      then it will receive 1 team from 123(1)+ 4 alone can form 1 team + but from where it will bring the another team?
      which was the main question => 1234(3)

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

    how to get intution either we have to use 1d dp array or 2d dp array? seems like long way to go. :(

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

      Same my problem

    • @Claire-uw1qv
      @Claire-uw1qv Před rokem +2

      use 1d array when u WANT duplicates or permutations, use 2d array when u WANT combinations and DONT want duplicates. if u want to know why then search this video title on yt :- "Coin Change Problems Analysis | Leetcode 322 v/s 518 Explained | Dynamic Programming In-Depth" by pepcoding

  • @harshyallewar5876
    @harshyallewar5876 Před 2 lety

    Very nice sir

    • @Pepcoding
      @Pepcoding  Před 2 lety

      Keep learning.
      And for better experience, visit nados.io, where you will get well curated content and career opportunities.

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

    sir ek hi dil hai , kitni baar jeetogey,
    sir, if i get placed in good company, then definitely all credit will go to you.
    bas ho jaye achi company mein placed, i daily do 4 question from your playlist , and watching it in order with full dedication and discipline,

    • @Pepcoding
      @Pepcoding  Před 3 lety

      Thankyou beta!
      I am glad you liked it. I hope that you are watching till the end and trying to understand what, how, and especially why of the problem.
      If you like our efforts, will you like to write a few words about us here (www.quora.com/How-do-I-start-learning-or-strengthen-my-knowledge-of-data-structures-and-algorithms )

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

      Did you get placed?

  • @deepanshverma3379
    @deepanshverma3379 Před 2 lety

    Janab(😅chaman datt) kisi ki bas ki ni ha Ki itna shandar material free da paye thank you sir

    • @Pepcoding
      @Pepcoding  Před 2 lety

      Glad you liked it:) Please visit nados.pepcoding.com for more curated content like this :)

  • @chandrakantshinde6
    @chandrakantshinde6 Před 3 lety

    respect++, ❤❤❤❤❤

    • @Pepcoding
      @Pepcoding  Před 3 lety

      Thankyou beta!
      I am glad you liked it. I hope that you are watching till the end and trying to understand what, how, and especially why of the problem.
      If you like our efforts, will you like to write a few words about us here (www.quora.com/What-are-the-good-websites-to-learn-data-structures-and-algorithms )

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

    take a bow !!!

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

    Amazing explanation, thanks.
    I understood the solution but how to get this thought process. What are the key points in the question which will push us to choose this approach. Why have we chosen this approach is not clear.

    • @suzanaangboo2448
      @suzanaangboo2448 Před rokem

      as he says in previous video, thought process come from practicing more and more questions.

  • @kms8320
    @kms8320 Před 2 lety

    sahab tu bahot mast kam karta he!!!😁 jokes apart you are a great teacher Sumeet sir.

    • @Pepcoding
      @Pepcoding  Před 2 lety

      Thanks a ton.
      For better experience sign up on nados.io

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

    Sir how can we able to drive the formula by their own? I am unable to think like you think.

    • @Pepcoding
      @Pepcoding  Před 3 lety +10

      2 tareeke hain
      1. +1, + 2 ka maths revise karke permutations and combinations mei ache ho jaie.
      2. 200 DP questions solve kar lijie.

    • @anubhavgupta4412
      @anubhavgupta4412 Před 3 lety

      @@Pepcoding sir kal tk mera yeh "DP" foundation wala khtam ho jayega to sir mujhe ab phle 200 ka count pura karna chaiye (leetcode+level up apka ) ya phir phle agey ka foundation complete krloon phir apka level 2 or leetcode se 200 ka count pura karoon !!

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

      @@anubhavgupta4412 did you completed foundation or level up

    • @anubhavgupta4412
      @anubhavgupta4412 Před 3 lety

      @@mickyman753 foundation completed . ..level up chl raha hai

    • @mickyman753
      @mickyman753 Před 3 lety

      @@anubhavgupta4412 bhai can you share how do you revise level1 ,and abhi kitna confidence aya hai level1 krne ke baad

  • @THEPOSTBYLOT
    @THEPOSTBYLOT Před 2 lety

    Sir could you please explain why is it not equivalent to nCk ie n!/k!(n-k)!

  • @anjalirai5337
    @anjalirai5337 Před 3 lety

    Sir one test case is failing with recursion
    import java.io.*;
    import java.util.*;
    public class Main {
    public static int part(int n,int k){
    if(n < k || k == 0 || n == 0){
    return 0;
    } else if(n == k || k == 1){
    return 1;
    }

    return part(n - 1 , k - 1) + part(n - 1 , k) * k;


    }
    public static void main(String[] args) throws Exception {
    Scanner scn = new Scanner(System.in);
    int n = scn.nextInt();
    int k = scn.nextInt();

    System.out.println(part(n,k));
    }
    }

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

    Sir itna observational formula skill chaiye!

  • @saarimkhan557
    @saarimkhan557 Před 3 lety

    Sir hame pata kaise chale ga ki kab double dimensional array use karna aur kab single dimensional array?

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

      for any problem first think for its recursion and in recursion when two dimensions are being varied then in those case 2-d dp is applied and
      if there is only single dimension being varied then 1 -d dp is applied....
      Also by doing more and more practice u will know where to use 1-d and where to use 2-d dp

    • @saarimkhan557
      @saarimkhan557 Před 3 lety

      Ok Sir understood

  • @kunalsihare5244
    @kunalsihare5244 Před 3 lety

    sir isme Why samaj nhi aya ? ki y formula kyu lagaya ??

  • @anjneykumarsingh4461
    @anjneykumarsingh4461 Před 3 lety

    Sir mtlb jyada question krne se hi problem solving ability bdhegi

  • @amandixit3555
    @amandixit3555 Před 3 lety

    sir iss vedio mei aur isse pichli vedio mei badhi confusion hogaya hai, can you please elaborate the previos two problems in a simple way

    • @Pepcoding
      @Pepcoding  Před 3 lety

      beta backtracking mei inki details hain. wo dekhne ke baad asaan lagega.

    • @anjneykumarsingh4461
      @anjneykumarsingh4461 Před 3 lety

      Bhai pnc mains k revise krle

  • @TrueSolutionsOfficial
    @TrueSolutionsOfficial Před 2 lety

    for n=0 and k=0 dp[0][0] =1
    this is because 0 person can be divided in 0 teams in 1 way.

    • @AmanTheMystery
      @AmanTheMystery Před rokem

      true,, but sir's solution works fine becoz dp[0][0] only matters for dp[1][1] and for that sir already placed for 1 team dp[1][i]=1; that's why it does not give wrong ans but for 0 team and for 0 persons answer will be one ,,,,,,sumit sir also teaches this in many recursion videos;

  • @geetanegi2736
    @geetanegi2736 Před 3 lety

    Sir mujha dp ka que hard lg rha hai . Mai roj 2 que krt hu dp ka mind headache hona lgta hai itna soch nhi pata.

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

      Koi nh beta, dp k questions tede hote h aur easily approach nh click krti but jaise jaise aap zda number of questions practice kroge to vo thinking process build hota jayega aur fr aap further nye questions khud se bna paoge.

    • @geetanegi2736
      @geetanegi2736 Před 3 lety

      Thank u sir jai hind jai bharat.

    • @shankysays
      @shankysays Před 2 lety

      Got placed?

  • @vaibhavtiwari6540
    @vaibhavtiwari6540 Před 3 lety

    Sir, if you tell the memoization and then let us convert it to dp, or even dont do it, the concept will make much more sense, also the question does not give tle for even pure recursion.

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

    sir in first explanation at 5 players and 4team we will get 10 as answer and not 11

    • @yashkushwaha8492
      @yashkushwaha8492 Před 3 lety

      yes you are right
      It should be
      0 0 0 0 0 0
      0 1 1 1 1 1
      0 0 1 3 7 15
      0 0 0 1 6 25
      0 0 0 0 1 10

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

    a
    w
    e
    s
    o
    m
    e
    cringe ke liye maaf karna