fynd || Onsite || Paint fence
Anonymous User
1353

There are n consecutives houses in the city. You are painting n houses with k different colors. You must paint the house following these rules:

  • Every house must be painted with exactly one color.
  • There cannot be three or more consecutive houses with the same color.

Given the two integers n and k, return the number of ways you can paint the houses.

Example:

Input: n = 3, k = 2
Output: 6

Explaination: Possible ways to paint the houses with 2 colors(0, and 1) are : 1 1 0, 1 0 1, 1 0 0, 0 1 1, 0 0 1, 0 1 0

Need help around this problem, I mean how to approach this kind of problem, is it through DP, graph, or maths?

Comments (5)