Paint Fence -- LeetCode

There is a fence with n posts, each post can be painted with one of the k colors.

You have to paint all the posts such that no more than two adjacent fence posts have the same color.

Return the total number of ways you can paint the fence.

Note:
n and k are non-negative integers.

思路:DP。

设p(i)表示从第一张海报到第i张海报的情况数。

初始情况:p(0) = 0,p(1) = k。

当n=2时,有两种情况:1)第二张海报的颜色与第一张不同,一共k * (k - 1)种情况;2)两张海报的颜色一样,一共k种情况。所以p(2) = k + k* (k - 1).

当n>2时,假设当前是第i张海报,此时有两种情况:1)第i张海报与前一张的颜色不同,一共是p(i - 1) * (k - 1)种情况; 2) 第i张海报与前一张的颜色一样,一共是p(i-2) * (k - 1)种情况。

所以p(i) = p(i-2)*(k-1) + p(i-1)*(k-1).

在实现时,我们并不需要用数组来记录所有的结果,我们只需要p(i-2)和p(i-1)的值,因此可以用变量slow来记录p(i-2)的值,用fast来记录p(i-1)的值。

 1 class Solution {
 2 public:
 3     int numWays(int n, int k) {
 4         if (n == 0) return n;
 5         if (n == 1) return k;
 6         int slow = k;
 7         int fast = slow + slow * (k - 1);
 8         for (int i = 3; i <= n; i++) {
 9             int cur = slow * (k - 1) + fast * (k - 1);
10             slow = fast;
11             fast = cur;
12         }
13         return fast;
14     }
15 };
时间: 2024-08-07 08:49:38

Paint Fence -- LeetCode的相关文章

276. Paint Fence

/* * 276. Paint Fence * 2016-6-26 by Mingyang * 上次paint了house,这次paint fence,因为我们这里的条件是最多连续两个有相同的颜色 * 那么这里就稍稍复杂了那么一丢丢: * 设S(n)表示当前杆和前一个杆颜色相同的个数,D(n)表示当前杆和前一个颜色不相同的个数. * 则递推关系式为: * S(n) = D(n - 1), 即若当前杆和前一个杆颜色相同的个数等于前一个杆和再前一个杆颜色不相同的个数. * D(n) = (k - 1

[Locked] Paint Fence

Paint Fence There is a fence with n posts, each post can be painted with one of the k colors. You have to paint all the posts such that no more than two adjacent fence posts have the same color. Return the total number of ways you can paint the fence

Leetcode: Paint Fence

There is a fence with n posts, each post can be painted with one of the k colors. You have to paint all the posts such that no more than two adjacent fence posts have the same color. Return the total number of ways you can paint the fence. Note: n an

[LeetCode#276] Paint Fence

Problem: There is a fence with n posts, each post can be painted with one of the k colors. You have to paint all the posts such that no more than two adjacent fence posts have the same color. Return the total number of ways you can paint the fence. N

[LeetCode] 276. Paint Fence 粉刷篱笆

There is a fence with n posts, each post can be painted with one of the k colors. You have to paint all the posts such that no more than two adjacent fence posts have the same color. Return the total number of ways you can paint the fence. Note:n and

*Paint Fence

There is a fence with n posts, each post can be painted with one of the k colors. You have to paint all the posts such that no more than two adjacent fence posts have the same color. Return the total number of ways you can paint the fence. public int

[LintCode] Paint Fence 粉刷篱笆

There is a fence with n posts, each post can be painted with one of the k colors.You have to paint all the posts such that no more than two adjacent fence posts have the same color.Return the total number of ways you can paint the fence. Notice n and

LintCode_514 Paint Fence

题目 here is a fence with n posts, each post can be painted with one of the k colors.You have to paint all the posts such that no more than two adjacent fence posts have the same color.Return the total number of ways you can paint the fence. 注意事项 n and

514. Paint Fence

There is a fence with n posts, each post can be painted with one of the k colors. You have to paint all the posts such that no more than two adjacent fence posts have the same color. Return the total number of ways you can paint the fence. Example Gi