276. Paint Fence π
Description
You are painting a fence of n
posts with k
different colors. You must paint the posts following these rules:
- Every post must be painted exactly one color.
- There cannot be three or more consecutive posts with the same color.
Given the two integers n
and k
, return the number of ways you can paint the fence.
Example 1:
Input: n = 3, k = 2 Output: 6 Explanation: All the possibilities are shown. Note that painting all the posts red or all the posts green is invalid because there cannot be three posts in a row with the same color.
Example 2:
Input: n = 1, k = 1 Output: 1
Example 3:
Input: n = 7, k = 2 Output: 42
Constraints:
1 <= n <= 50
1 <= k <= 105
- The testcases are generated such that the answer is in the range
[0, 231 - 1]
for the givenn
andk
.
Solutions
Solution 1: Dynamic Programming
We define \(f[i]\) to represent the number of ways to paint the fence posts from \([0..i]\) such that the last two posts have different colors, and \(g[i]\) to represent the number of ways to paint the fence posts from \([0..i]\) such that the last two posts have the same color. Initially, \(f[0] = k\) and \(g[0] = 0\).
When \(i > 0\), we have the following state transition equations:
The final answer is \(f[n - 1] + g[n - 1]\).
The time complexity is \(O(n)\) and the space complexity is \(O(n)\), where \(n\) is the number of fence posts.
1 2 3 4 5 6 7 8 9 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
1 2 3 4 5 6 7 8 9 10 |
|
1 2 3 4 5 6 7 8 9 10 |
|
Solution 2: Dynamic Programming (Space Optimization)
We notice that \(f[i]\) and \(g[i]\) are only related to \(f[i - 1]\) and \(g[i - 1]\). Therefore, we can use two variables \(f\) and \(g\) to record the values of \(f[i - 1]\) and \(g[i - 1]\) respectively, thus optimizing the space complexity to \(O(1)\).
1 2 3 4 5 6 7 8 |
|
1 2 3 4 5 6 7 8 9 10 11 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
1 2 3 4 5 6 7 |
|
1 2 3 4 5 6 7 8 9 |
|