zoukankan      html  css  js  c++  java
  • *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 numWays(int n, int k) {
        if(n == 0) return 0;
        else if(n == 1) return k;
        int diffColorCounts = k*(k-1);
        int sameColorCounts = k;
        for(int i=2; i<n; i++) {
            int temp = diffColorCounts;
            diffColorCounts = (diffColorCounts + sameColorCounts) * (k-1);
            sameColorCounts = temp;
        }
        return diffColorCounts + sameColorCounts;
    }

    We divided it into two cases.

    1. the last two posts have the same color, the number of ways to paint in this case issameColorCounts.

    2. the last two posts have different colors, and the number of ways in this case isdiffColorCounts.

    The reason why we have these two cases is that we can easily compute both of them, and that is all I do. When adding a new post, we can use the same color as the last one (if allowed) or different color. If we use different color, there're k-1 options, and the outcomes shoule belong to the diffColorCounts category. If we use same color, there's only one option, and we can only do this when the last two have different colors (which is the diffColorCounts). There we have our induction step.

    Here is an example, let's say we have 3 posts and 3 colors. The first two posts we have 9 ways to do them, (1,1), (1,2), (1,3), (2,1), (2,2), (2,3), (3,1), (3,2), (3,3). Now we know that

    diffColorCounts = 6;

    and

    sameColorCounts = 3;

    Now for the third post, we can compute these two variables like this:

    If we use different colors than the last one (the second one), these ways can be added intodiffColorCounts, so if the last one is 3, we can use 1 or 2, if it's 1, we can use 2 or 3, etc. Apparently there are (diffColorCounts + sameColorCounts) * (k-1) possible ways.

    If we use the same color as the last one, we would trigger a violation in these three cases (1,1,1), (2,2,2) and (3,3,3). This is because they already used the same color for the last two posts. So is there a count that rules out these kind of cases? YES, the diffColorCounts. So in cases withindiffColorCounts, we can use the same color as the last one without worrying about triggering the violation. And now as we append a same-color post to them, the former diffColorCountsbecomes the current sameColorCounts.

    Then we can keep going until we reach the n. And finally just sum up these two variables as result.

    Hope this would be clearer.

    reference: https://leetcode.com/discuss/56173/o-n-time-java-solution-o-1-space

    
    
  • 相关阅读:
    Python迭代器的反复使用
    快速求幂模运算实现
    rural lifestyle & city lifestyle
    Python实现 扩展的欧几里德算法求(模逆数)最大公约数
    jupyter themes一行命令设置个人最爱界面
    python数组、矩阵相乘的多种方式
    有一组整型数,其中除了2个数字以外的其它数字都是俩俩成对出现的,编写程序找出这2个不成对出现的数字。
    Linux线程池技术处理多任务
    编写函数求两个整数 a 和 b 之间的较大值。要求不能使用if, while, switch, for, ?: 以 及任何的比较语句。
    C++const类型的引用参数
  • 原文地址:https://www.cnblogs.com/hygeia/p/5126067.html
Copyright © 2011-2022 走看看