Paint Fence
Description:
There is a fence with n posts, each post can be painted with one of the k colors.
You have to paint all the posts such that no more than two adjacent fence posts have the same color.
Return the total number of ways you can paint the fence.
Example
Given n=3, k=2 return 6
post 1, post 2, post 3
way1 0 0 1
way2 0 1 0
way3 0 1 1
way4 1 0 0
way5 1 0 1
way6 1 1 0
Code:
1.
超时报错
class Solution:
"""
@param n: non-negative integer, n posts
@param k: non-negative integer, k colors
@return: an integer, the total number of ways
"""
def numWays(self, n, k):
# write your code here
if n==0 or k == 0:
return 0
if n == 1:
return k
elif n == 2:
return k*k
else:
n_2 = self.numWays(n-2, k)
n_1 = self.numWays(n-1, k)
return (n_1+n_2)*(k-1)
2.
class Solution:
"""
@param n: non-negative integer, n posts
@param k: non-negative integer, k colors
@return: an integer, the total number of ways
"""
def numWays(self, n, k):
# write your code here
dp = [0, k , k*k, 0]
if n <= 2:
return dp[n]
for i in range(2, n):
dp[3] = (k - 1) * (dp[1] + dp[2])
dp[1] = dp[2]
dp[2] = dp[3]
return dp[3];