Python 6 lines


  • 0
    M
    class Solution(object):
        def numWays(self, n, k):
            same, diff = 0, k
            for i in range(n-1):
                same, diff = diff, (same + diff) * (k - 1)
            return same + diff * (n != 0)
    

Log in to reply
 

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