filmov
tv
Partition to K Equal Sum Subsets - source code & running time recurrence relation

Показать описание
Partition a set of positive integers into K subsets, such that the sums of the numbers of each subset are equal.
698. Partition to K Equal Sum Subsets
So the function doesn’t just return true or false based on whether it is possible to create K partitions or not. But actually creates those partitions. We assume, these are multi-sets which allow for duplicate values. In other words, the numbers are not required to be unique.
Last week's episode that tackles the Partition Problem of exactly 2 subsets:
Source code of implementation in Java:
The number of partitions does not have to be some small constant. In fact, it could be as large as n. In that case, the recurrence relation is:
T(n) = n * T(n-1)
T(n-1) = (n-1) * T(n-2)
T(n) = n * (n-1) * T(n-2)
T(n) = n * (n-1) * (n-2) * … T(0) = O(n!)
Which leads to running time of O(n!)
Wikipedia:
Not to be confused with
in which the size of the sets is 3 (you have triplets of numbers that add up to some target). Thanks, @Vadim for clarification.
Written and narrated by Andre Violentyev
698. Partition to K Equal Sum Subsets
So the function doesn’t just return true or false based on whether it is possible to create K partitions or not. But actually creates those partitions. We assume, these are multi-sets which allow for duplicate values. In other words, the numbers are not required to be unique.
Last week's episode that tackles the Partition Problem of exactly 2 subsets:
Source code of implementation in Java:
The number of partitions does not have to be some small constant. In fact, it could be as large as n. In that case, the recurrence relation is:
T(n) = n * T(n-1)
T(n-1) = (n-1) * T(n-2)
T(n) = n * (n-1) * T(n-2)
T(n) = n * (n-1) * (n-2) * … T(0) = O(n!)
Which leads to running time of O(n!)
Wikipedia:
Not to be confused with
in which the size of the sets is 3 (you have triplets of numbers that add up to some target). Thanks, @Vadim for clarification.
Written and narrated by Andre Violentyev
Комментарии