Similar Problems

Similar Problems not available

Number Of Beautiful Partitions - Leetcode Solution

Companies:

LeetCode:  Number Of Beautiful Partitions Leetcode Solution

Difficulty: Hard

Topics: string dynamic-programming  

The "Number of Beautiful Partitions" problem on LeetCode is a problem of combinatorics involving the concept of partitions of a given integer. The problem is to find the number of partitions of a given integer n into beautiful partitions. A beautiful partition is a partition in which at least one element is divisible by k (i.e., a multiple of k) and at least one element is not divisible by k, where k is a given positive integer.

For example, suppose the input integer n is 5 and k is 2. Then the partitions of n are {5}, {4, 1}, {3, 2}, and {2, 2, 1}. Out of these four partitions, only {4, 1} and {2, 2, 1} are beautiful partitions because they contain at least one element that is divisible by 2 and at least one element that is not divisible by 2. Therefore, the number of beautiful partitions of 5 is 2.

The solution to this problem can be achieved using dynamic programming. We can define a 2D table dp[i][j] to represent the number of beautiful partitions of i using j as the smallest element. The answer to the problem will be the sum of all dp[n][j] for j from 1 to n.

To populate the table dp, we can use the following recurrence relation:

dp[i][j] = dp[i-j][j] + dp[i][j+1]

Here, the first term on the right side of the equation represents the number of beautiful partitions of i that contain j as the smallest element. We can obtain this value by considering all partitions of i that contain j and reducing the problem to finding the number of beautiful partitions of i-j using j as the smallest element. The second term on the right side of the equation represents the number of beautiful partitions of i that do not contain j as the smallest element. We can obtain this value by considering all partitions of i that do not contain j and reducing the problem to finding the number of beautiful partitions of i using the next smallest element greater than j.

The base cases for the recurrence relation are dp[0][j] = 1 (there is one way to partition 0) and dp[i][i] = 1 (there is only one partition of i that contains i as the smallest element, namely {i}).

We can implement this algorithm in Python as follows:

def count_beautiful_partitions(n, k): dp = [[0 for j in range(n+1)] for i in range(n+1)] for j in range(1, n+1): dp[0][j] = 1 dp[j][j] = 1 for i in range(1, n+1): for j in range(1, i): dp[i][j] = dp[i-j][j] + dp[i][j+1] count = 0 for j in range(1, n+1): if j % k != 0: count += dp[n][j] return count

In this implementation, we first create the 2D table dp and initialize the base cases. We then populate the rest of the table using the recurrence relation. Finally, we loop through all values of j that are not divisible by k, summing the number of beautiful partitions of n that do not contain j as the smallest element. This sum is returned as the answer to the problem.

Overall, the time complexity of this algorithm is O(n^2), as we need to fill in an n x n table. The space complexity is also O(n^2), as we need to store the entire table.

Number Of Beautiful Partitions Solution Code

1