276. Paint Fence

276- Paint Fence
**My SubmissionsQuestion
Editorial Solution

Total Accepted: 10079 Total Submissions: 31886 Difficulty: Easy

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.

Hide Company Tags
Google
Hide Tags
Dynamic Programming
Hide Similar Problems
(E) House Robber (M) House Robber II (M) Paint House (H) Paint House II

  
    public int numWays(int n, int k) {
     /* idea:  e.g.   n = 3, k = 2 (r,w)
         two fences, two color: (r, r), (r, w) (w, r), (w, w)
         diff2 = k * (k - 1) = 2 * ( 2- 1)  = 2
         same2 = k = 2
         total2 = diff2 + same2 = 2 + 2 = 4
         
         three fences, two color:
         same3 = diff2;
         diff3 = (total) * (k-1) = (diff2 + same2) * (k -1)
         ref: https://discuss.leetcode.com/topic/23426/o-n-time-java-solution-o-1-space
     */
       if(n == 0) {
           return 0;
       } else if(n == 1) {
           return k;
       }
       
      int diffCounts = k * (k -1) ; // n = 2
      int sameCounts = k;
      
      for (int i = 2 ; i < n; i++) {  // n > 2
          int temp = diffCounts;
          diffCounts = (diffCounts + sameCounts) * ( k - 1);
          sameCounts = temp;
      }
        return diffCounts + sameCounts;
    }
    原文作者:billyzhang
    原文地址: https://www.jianshu.com/p/02e8225e048b
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞