Partition Problem - 2 subsets of equal sum, as closely as possible - tutorial and source code

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ม.ค. 2025

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

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

    Ahh, the magic of recursion. I struggled with the explenation a bit, but I think i get it now. But like you said, it's a very slow solution so we need to do better. It makes a lot of sense that this problem can be reduced to the Knapsack problem. Time to rewatch your Knapsack video :)

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

    Your videos are just amazing!! Thank you SO much, sir. Your way of explaining hard, complex problems in such a laid-back manner is enthralling to watch!! It's a pleasure subscribing to your channel. Thank you Sir!!!

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

      Happy to help and thanks for the compliment!

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

    U are so good at explaining the concepts properly

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

    Beautiful explanation, I look forward to your future videos

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

      Thanks! More videos are in the making =)

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

    Elegant explanation - thanks for the nice video

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

    Thank you so much for this! I have a suggestion for a video maybe; there is barely any info on youtube about bounded knapsack and how to solve it; it took me a good week to understand one of the blogs regarding the topic; could you please make a video on that?

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

      Thank you very much for the video topic suggestion, I do appreciate it! When you say "bounded knapsack", is that different from the typical 0/1 knapsack problem? I ask because that one I covered already here: th-cam.com/video/-kedQt2UmnE/w-d-xo.html

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

    There is interesting solution that works in O(S*sqrt(S)) where S is sum of the set

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

      Oh cool! Could you please elaborate a little?

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

      We already can solve this problem in O(S * n). So now we are trying to reduce the number of elements in set.
      Let K be the number of *unique* elements in set with sum S. Then there is a fact that K

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

      @@stablesort it is useful where you have constraints something like (N < 10^5, S < 2*10^5), if you think about possible sets, you would realise that set must have tons of duplicates

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

    Congratulation 😁😍.please put some more video of dynamic programming. Subset sum, finding the path

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

      Sounds good, thanks! By the way, I did make a follow up video to this one that you may like, called "Partition K Equal Subset Sum":
      th-cam.com/video/DB-9JlnbBpM/w-d-xo.html

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

    Very nice and lucid. Thanks.

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

      Thanks for the compliment!

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

    Union Find aka DSU could have been also used in such cases?

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

    Amazing sir !!
    Just Amazing

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

      Thanks! Glad to see you enjoyed it =)

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

    Thanks, sir, it helps my understanding a lot :)

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

      Glad to hear it!

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

    Thanks for a great explanation!
    Suggested topic: There are some DP problems where we have two sets of size N1 and N2, and we choose N1 as one dimension and 1

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

      Thanks for suggesting a topic! I do appreciate it and will look into it. I haven't done anything with bitmasks on this channel, so it'll be a good addition =)

    • @0anant0
      @0anant0 4 ปีที่แล้ว

      @@stablesort Thanks! An example of this is: LeetCode 1434. Number of Ways to Wear Different Hats to Each Other

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

    What about if result is negative after subtract value of i from target?

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

    Sir ,
    What happened if the weight of the bag is
    5^24 then at that condition how would I applied knapsack because knapsack required 0(n*m) auxiliary space I think at that condition I have to go with recursive + backtracking
    Is this a right choice ?

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

      Yes, that's exactly right: if the capacity of the bag is very large, then creating a giant table becomes a problem. Ultimately, this is an NP-complete problem, so there are limits to what could be computed.

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

    Велике Вам дякую!

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

      Будь ласка!

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

    Suppose your numbers are 1, 10. If you divide the sum of them by 2 you get 5.5 even if you round up the result to 6, then 2 partitions each of size 6 can't contain the number 10.

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

    the way my professor explained it was very confusing

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

    can we do it in linear time ?

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

      No, not in the general case.

  • @Niharika-kt4qs
    @Niharika-kt4qs ปีที่แล้ว

    How does 1+10 = 5+7 sir😢

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

    This problem was today's codejam. Very few could solve.

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

      I hope you solved it!

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

      @@stablesort your explanation helped 👍