Monday, November 16, 2015

[leetcode] Paint Fence


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public class Solution {
    public int numWays(int n, int k) {
        if (n == 0 || k == 0) return 0;
        if (n == 1) return k;
        int diff = k*(k-1);
        int same = k;
        for (int i = 3; i <= n; i++){
         int p_same = same;
         same = diff;
         diff = p_same*(k-1)+diff*(k-1);
        }

        return diff+same;
    }
}

No comments:

Post a Comment