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 k are non-negative integers.

链接: http://leetcode.com/problems/paint-fence/

题解:

又是数学题,给篱笆涂色,相邻最多两个post可以同色。第一思路就是Dynamic Programming了。代码大都参考了Discuss的Jenny_Shaw的。要注意的就是每次计算, 当前的结果应该等于sameColor和differentColor的和,而differentColor只能在k - 1种color里选,等于之前结果 * (k - 1), sameColor等于之前的differentColor,之后进行下一次计算。

Time Complexity - O(n), Space Complexity - O(1)

public class Solution {
    public int numWays(int n, int k) {
        if(n <= 0 || k <= 0) {
            return 0;
        }
        if(n == 1) {
            return k;
        }
        int sameColor = k;
        int differentColor = k * (k - 1);
        
        for(int i = 2; i < n; i++) {
            int tmp = differentColor;
            differentColor = (sameColor + differentColor) * (k - 1);
            sameColor = tmp;
        }
        
        return sameColor + differentColor;
    }
}

 

二刷:

依然是使用dp。题目给定最多两个fence可以用一种颜色喷漆。下面我们来仔细分析一下。

  1. 首先我们判断边界的条件,n <=0,  k <= 0, n == 1
  2. 我们初始化两个变量,sameColorLastTwo和diffColorLastTwo, 假如位于0和1位置的两个fence用一种颜色喷的话,那么我们可以设定sameColorLastTwo = k,  假如它们用两种颜色喷的话,那么我们可以设定diffColorLastTwo = k * (k - 1)
  3. 接下来我们从i到n开始遍历,我们设定diffColorLastTwo + sameColorLastTwo等于到第i位之前,我们一种有多少种喷漆方法
    1. 先建立一个tmp保存当前的diffColorLastTwo,也就是i-1位与i-2位使用不同颜色
    2. 假如我们我们第i位,不使用与第i-1位相同的颜色,那么我们更新diffColorLastTwo = (diffColorLastTwo + sameColorLastTwo) * (k - 1)
    3. 假如我们第i位使用和第i - 1位相同的颜色,那么第i位的sameColorLastTwo = 第i - 1位的diffColorLastTwo 
  4. 遍历完第n-1位后返回结果sameColorLastTwo + diffColorLastTwo

Java:

Time Complexity - O(n), Space Complexity - O(1)

public class Solution {
    public int numWays(int n, int k) {
        if (n <= 0 || k <= 0) {
            return 0;
        }
        if (n == 1) {
            return k;
        }
        int sameColorLastTwo = k;
        int diffColorLastTwo = k * (k - 1);
        for (int i = 2; i < n; i++) {
            int tmp = diffColorLastTwo;
            diffColorLastTwo = (sameColorLastTwo + diffColorLastTwo) * (k - 1);
            sameColorLastTwo = tmp;
        }
        return sameColorLastTwo + diffColorLastTwo;
    }
}

 

三刷:

换了一点点写法,看起来更简洁,不过最后多做了两次计算操作。

Java:

public class Solution {
    public int numWays(int n, int k) {
        if (n <= 0 || k <= 0) return 0;
        if (n == 1) return k;
        int res = 0;
        int sameColorLastTwo = k, diffColorLastTwo = k * (k - 1);
        for (int i = 2; i <= n; i++) {
            res = sameColorLastTwo + diffColorLastTwo;
            sameColorLastTwo = diffColorLastTwo;
            diffColorLastTwo = res * (k - 1);
        }
        return res;
    }
}

 

 

Reference:

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

https://leetcode.com/discuss/58451/java-dp-solution

https://leetcode.com/discuss/58879/python-solution-with-explanation

https://leetcode.com/discuss/56245/lucas-formula-maybe-o-1-and-3-4-liners

https://leetcode.com/discuss/62587/7-lines-no-special-case-code-o-n-o-1

 

posted @ 2015-12-10 01:35  YRB  阅读(867)  评论(0编辑  收藏  举报