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) * (D(n - 1) + S(n - 1)),即前一个杆和再前一个杆所有可能的颜色组合,
     * 乘以当前杆与前一个杆颜色不相同的个数,即(k - 1)。用两个变量记录D和S,并交替更新即可。
     * -------------这里需要注意所有情况:
     * 1.当前杆和前一个杆颜色相同S(n) = D(n - 1)
     * 2.当前杆和前一个颜色不相同D(n) = (k - 1) * (D(n - 1) + S(n - 1))
     */
    public int numWays(int n, int k) {
        if (n == 0 || k == 0) return 0;
        if (n == 1) return k;
        // same[i] means the ith post has the same color with the (i-1)th post.
        int[] same = new int[n];
        // diff[i] means the ith post has a different color with the (i-1)th post.
        int[] diff = new int[n];
        same[0] = same[1] = k;
        diff[0] = k;
        diff[1] = k * (k - 1);
        for (int i = 2; i < n; ++i) {
            same[i] = diff[i-1];
            diff[i] = (k - 1) * same[i-1] + (k - 1) * diff[i-1];
        }
        return same[n-1] + diff[n-1];
    }

				
时间: 2024-08-10 19:43:30

276. Paint Fence的相关文章

[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

[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

*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] 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

[LeetCode] 276. Paint Fence_Easy tag: Dynamic Programming

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