-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPainting-fence-algorithm.py
78 lines (56 loc) · 2.07 KB
/
Painting-fence-algorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Using recursion
def paint_recursion(n, k):
# Base case: if n == 1
if n == 1:
return k
# Base case: if n == 2
if n == 2:
return k + (k * (k - 1))
# Call function recursively
ans = (k - 1) * (paint_recursion(n - 2, k) + paint_recursion(n - 1, k))
return ans
# Using Top-Down Dynamic Programming (Memoization)
def paint_memoization(n, k, memo={}):
# Base case: if n == 1 or n == 2
if n == 1:
return k
if n == 2:
return k + (k * (k - 1))
# Check if the result already computed or not
if n in memo:
return memo[n]
memo[n] = (k - 1) * (paint_memoization(n - 2, k, memo) + paint_memoization(n - 1, k, memo))
return memo[n]
# Using Bottom-Up Dynamic Programming (Tabulation)
def paint_tabulation(n, k):
# Initialize a dp array
dp = [0] * (n + 1)
# Base cases
dp[1], dp[2] = k, k + (k * (k - 1))
# Fill the dp array iteratively
for i in range(3, n + 1):
dp[i] = (k - 1) * (dp[i - 2] + dp[i - 1])
return dp[n]
# Space optimized solution
def paint_space_optimized(n, k):
# Initialize two variables to store base case values
prev2, prev1 = k, k + (k * (k - 1))
for _ in range(3, n + 1):
prev2, prev1 = prev1, (k - 1) * (prev2 + prev1)
return prev1
"""
Problem Statement:
Ninja has given a fence, and he gave a task to paint this fence.
The fence has 'N' posts and Ninja has 'K' colors. Ninja wants to paint the fence so that not more than two adjacent posts have same color.
Tasks is to find the number of ways that Ninja can paint the fence
"""
if __name__ == '__main__':
n, k = 3, 2 # output : 6
# Test the recursive function
print(f"Total number of ways: {paint_recursion(n, k)}\n")
# Test the Top-down DP(Memoization) function
print(f"Total number of ways: {paint_memoization(n, k)}\n")
# Test the Bottom-up DP(Tabulation) function
print(f"Total number of ways: {paint_tabulation(n, k)}\n")
# Test the space optimized function
print(f"Total number of ways: {paint_space_optimized(n, k)}\n")