Download Paint Fence.py
class Solution(object): # def numWays(self, n, k): # """ # :type n: int # :type k: int # :rtype: int # """ # w = [0, k, k*k] # while len(w) <= n: # w += sum(w[-2:]) * (k-1), # return w[n] def numWays(self, n, k): if n == 0: return 0 elif n == 1: return k # two step dp # ways = k # ways = k * k # ways[i>2] = (ways[i-1] + ways[i-2]) * (k - 1) dp =  * 2 dp = k dp = k * k for i in range(2, n): temp = dp dp = sum(dp) * (k - 1) dp = temp return dp
Leetcode 276 Paint Fence problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.
Feedback is the most important part of any website.
If you have any query, suggestion or feedback, Please feel free to contact us.