Clean c++ solution


  • 0
    1. Separate into 2 cases on whether current one has the same color with prev
    int numWays(int n, int k) {
            if (n == 0)
                return 0;
            int notadj = k, adj = 0;
            while (--n) {
                int nextadj = notadj;
                notadj = (k - 1) * (notadj + adj);
                adj = nextadj;
            }
            return notadj + adj;
    }
    

Log in to reply
 

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