C++ 4 lines solution


  • 0
        int numWays(int n, int k) {
            vector<int>v(n+1,0);
            v[0]=0,v[1]=k,v[2]=k*k;
            for(int i=3;i<n+1;i++) v[i]=(v[i-1]+v[i-2])*(k-1);
            return v[n];
        }
    

Log in to reply
 

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