LintCode Q514 Paint Fence in Python

  • Jinhai ZHOU
  • 2 Minutes
  • 2016年12月23日
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution:
# @param {int} n non-negative integer, n posts
# @param {int} k non-negative integer, k colors
# @return {int} an integer, the total number of ways
def numWays(self, n, k):
# Write your code here
# Dynamique programming, like fibonacci array
if n == 0:
return 0
if n == 1:
return k
same_way = 0
diff_way = k
total_way = k
for i in xrange(1, n):
same_way = diff_way
diff_way = total_way * (k - 1)
total_way = same_way + diff_way
return total_way
知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。