package com.fishercoder.solutions;

/**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.*/

public class _276 {
    public int numWays(int n, int k) {
        if (n == 0) {
            return 0;
        } else if (n == 1) {
            return k;
        }
        int sameColorCnt = k;
        int diffColorCnt = k * (k - 1);
        for (int i = 2; i < n; i++) {
            int temp = diffColorCnt;
            diffColorCnt = (diffColorCnt + sameColorCnt) * (k - 1);
            sameColorCnt = temp;
        }
        return sameColorCnt + diffColorCnt;
    }
}

@@ -32,16 +32,39 @@
 ns;%0A%0A/**
+%0A * 276. Paint Fence%0A%0A 
 There is
@@ -137,16 +137,16 @@
 colors.%0A
-
 %0A You ha
@@ -366,16 +366,56 @@
  _276 %7B%0A
+    public static class Solution1 %7B%0A    
     publ
@@ -453,16 +453,20 @@
         
+    
 if (n ==
@@ -479,24 +479,28 @@
             
+ 
+   
 return 0;%0A  
@@ -505,16 +505,20 @@
         
+   
+ 
 %7D else i
@@ -538,24 +538,28 @@
             
+    
 return k;%0A  
@@ -556,34 +556,42 @@
 turn k;%0A        
-%7D%0A
+    %7D%0A    
         int same
@@ -608,24 +608,28 @@
  k;%0A        
+    
 int diffColo
@@ -656,16 +656,20 @@
         
+   
+ 
 for (int
@@ -698,24 +698,28 @@
             
+    
 int temp = d
@@ -743,16 +743,20 @@
         
+ 
+   
 diffColo
@@ -815,16 +815,20 @@
         
+    
 sameColo
@@ -848,26 +848,32 @@
         
-%7D%0A
+    %7D%0A  
         
 return s
@@ -860,24 +860,26 @@
 %7D%0A          
+  
 return sameC
@@ -898,20 +898,30 @@
 ffColorCnt;%0A
+        %7D%0A
     %7D%0A%7D%0A

refactor 276