Simple Java solution


  • 0
    M
    public class Solution {
        public int numWays(int n, int k) {
            if(k==0)
            return 0;
            if(n==0)
            return 0;
            if(n==1)
            return k;
            if(n==2)
            return k*k;
            int[] posts=new int[n+1];
            posts[0]=1;
            posts[1]=k;
            posts[2]=k*k;
            
            for(int i=3;i<=n;i++){
                posts[i]=(k-1)*(posts[i-2]+posts[i-1]);
                
            }
            return posts[n]>0?posts[n]:0;
            
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.