lintcode-easy-Paint Fence

There is a fence with n posts, each post can be painted with one of the kcolors.
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 k are non-negative integers.

Example

Given n=3, k=2 return 6

      post 1,   post 2, post 3
way1    0         0       1
way2    0         1       0
way3    0         1       1
way4    1         0       0
way5    1         0       1
way6    1         1       0
public class Solution {
    /**
     * @param n non-negative integer, n posts
     * @param k non-negative integer, k colors
     * @return an integer, the total number of ways
     */
    public int numWays(int n, int k) {
        // Write your code here

        int[] diff = new int[n + 1];
        int[] same = new int[n + 1];

        diff[0] = 0;
        diff[1] = k;

        same[0] = 0;
        same[1] = 0;

        for(int i = 2; i <= n; i++){
            diff[i] = same[i - 1] * (k - 1) + diff[i - 1] * (k - 1);
            same[i] = diff[i - 1];
        }

        return diff[n] + same[n];
    }
}
时间: 2024-10-29 19:08:30

lintcode-easy-Paint Fence的相关文章

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

[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

[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: 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

*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

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

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

[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

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