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.

样例:

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

分析:

影响第n种的颜色的是第n-1与第n-2种颜色的,因此可能性都为k-1种
因此得到如下递推式:maxWays[n] = (k - 1) * (maxWays[n - 1] + maxWays[n - 2]);

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) {
        if(n == 0 || k == 0)
            return 0;
        if(n == 1)
            return k;
        
        int[] maxWays = new int[n + 1];
        maxWays[0] = 0;
        maxWays[1] = k;
        maxWays[2] = k * k;
        for(int i = 3;i <= n;i ++)
        {
            maxWays[i] = (k - 1) * (maxWays[i - 1] + maxWays[i - 2]);
        }
        
        return maxWays[n];
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容