![]() |
VOOZH | about |
There are 'p' balls of type P, 'q' balls of type Q and 'r' balls of type R. Using the balls we want to create a straight line such that no two balls of the same type are adjacent.
Examples :
Input: p = 1, q = 1, r = 0
Output: 2
Explanation: There are only two arrangements PQ and QPInput: p = 1, q = 1, r = 1
Output: 6
Explanation: There are only six arrangements PQR, QPR, QRP, RQP, PRQ and RPQInput: p = 2, q = 1, r = 1
Output: 12
Explanation: There are twelve arrangements PQRP, PRQP, QPRP, RPQP, PQPR, PRPQ, QPQR, RQRP, PQRQ, QRQP, RPQR, and QRPR
Table of Content
To solve this problem, we use a recursive approach, placing the last ball as one of three types. Each ball type is represented by a digit in the recurrence relation: 0 for P, 1 for Q, and 2 for R. If the last ball is of type P (0), the next ball must be of type Q or R, and similarly for the last balls of type Q (1) and R (2).
Recurrence Relation:
- if the last ball is of type P: countWays(p, q, r, last) = countWays(p-1, q, r, 1) + countWays(p-1, q, r, 2)
- if the last ball is of type Q: countWays(p, q, r, last) = countWays(p, q-1, r, 0) + countWays(p, q-1, r, 2)
- if the last ball is of type R: countWays(p, q, r, last) = countWays(p, q, r-1, 0) + countWays(p, q-1, r-1, 1)
Base Cases:
if (p < 0), (q < 0) or (r < 0) then countWays(p, q, r, last) = 0. if only one ball ramains of specific type, then:
- countWays(p, q, r, 0) = 1 if p=1 and q=0 and r=0
- countWays(p, q, r, 1) = 1 if p=0 and q=1 and r=0
- countWays(p, q, r, 2) = 1 if p=0 and q=0 and r=1
6
If we notice carefully, we can observe that the above recursive solution holds the following two properties of Dynamic Programming.
1. Optimal Substructure:
The solution to the problem of arranging balls with no two adjacent balls of the same type can be derived from the optimal solutions of smaller subproblems.
2. Overlapping Subproblems:
In the recursive solution, we notice that many subproblems are solved multiple times. For example, when calculating countWays(p, q, r, last), the same values of p, q, r are computed multiple times.
6
The approach is similar to the previous one. just instead of breaking down the problem recursively, we iteratively build up the solution by calculating in bottom-up manner.
So we will create 4D array of size (p+1)*(q+1)*(r+1)*last. and the state dp[i][j][k][l] will represent the number of ways to arrange the balls such that:
- i is the count of balls of type P placed so far.
- j is the count of balls of type Q placed so far.
- k is the count of balls of type R placed so far.
- l is the type of the last ball placed (l = 0 for P, l = 1 for Q, and l = 2 for R).
1. For the case where the last ball placed is of type P (i.e., l=0): if i>0, then dp[i][j][k][0] = dp[i-1][j][k][1] + dp[i-1][j][k][2]
2. For the case where the last ball placed is of type Q (i.e. ,l=1): if j>0, then dp[i][j][k][1]=dp[i][j-1][k][0]+dp[i][j-1][k][2]
3. For the case where the last ball placed is of type R (i.e., l=2): if k>0, then dp[i][j][k][2]=dp[i][j][k-1][0]+dp[i][j][k-1][1]
Base Cases
- dp[1][0][0][0]=1 (Only one P ball left, and the last placed ball is P)
- dp[0][1][0][1]=1 (Only one Q ball left, and the last placed ball is Q)
- dp[0][0][1][2]=1 (Only one R ball left, and the last placed ball is R)
6