




Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
We will use Dynamic programming approach. we will try all possible configuration.
Let dp[idx][prev1col][prev2col][prev3col] be the number of ways to color the rows of the grid from idx to n-1 keeping in mind that the previous row (idx - 1) has colors prev1col, prev2col and prev3col. Build the dp array to get the answer.