Skip to content

Commit 04f02ad

Browse files
authored
Painting fence Algorithm
program of implement the algorithm of painting fence in python
1 parent 39b1d5b commit 04f02ad

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

PaintingfenceAlgo

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Python3 program for Painting Fence Algorithm
2+
# optimised version
3+
4+
# Returns count of ways to color k posts
5+
def countWays(n, k):
6+
7+
dp = [0] * (n + 1)
8+
total = k
9+
mod = 1000000007
10+
11+
dp[1] = k
12+
dp[2] = k * k
13+
14+
for i in range(3,n+1):
15+
dp[i] = ((k - 1) * (dp[i - 1] + dp[i - 2])) % mod
16+
17+
return dp[n]
18+
19+
# Driver code
20+
n = 3
21+
k = 2
22+
print(countWays(n, k))
23+
24+

0 commit comments

Comments
 (0)