Painting Fence Algorithm in Python Last Updated : 28 May, 2024 Summarize Suggest changes Share Like Article Like Report Given a fence with n posts and k colors, find out the number of ways of painting the fence such that at most 2 adjacent posts have the same color. Since the answer can be large, return it modulo 10^9 + 7. Input : n = 2 k = 4Output : 16Explanation: We have 4 colors and 2 posts.Ways when both posts have same color : 4 Ways when both posts have diff color :4(choices for 1st post) * 3(choices for 2nd post) = 12 Input : n = 3 k = 2Output : 6 What is a Painting Fence Algorithm?The Painting Fence algorithm addresses a common combinatorial problem: Given a fence with n posts, how many ways can you paint the fence with k colors such that no more than two adjacent posts have the same color? This problem can be easily solved using Dynamic Programming. Algorithm: Create an array dp of size n+1 to store the number of ways to paint the fence for each number of posts.Define mod as 1000000007 to handle large numbers.dp[1] = k: If there is only one post, it can be painted in k ways.dp[2] = k * k: If there are two posts, there are k * k ways to paint them because each post can be painted independently with any of the k colors.Iterate from i = 3 to i = n:Use the formula: dp[i] = ((k - 1) * (dp[i - 1] + dp[i - 2])) % mod to calculate the number of ways to paint i posts:(k - 1): The current post can be painted in any of the k - 1 colors different from the previous one.(dp[i - 1] + dp[i - 2]): Add the ways to paint the previous post configurations.The number of ways to paint n posts is found in dp[n].Below is the implementation of the above algorithm: Python # Python3 program for Painting Fence Algorithm # optimised version # Returns count of ways to color k posts def countWays(n, k): dp = [0] * (n + 1) total = k mod = 1000000007 dp[1] = k dp[2] = k * k for i in range(3,n+1): dp[i] = ((k - 1) * (dp[i - 1] + dp[i - 2])) % mod return dp[n] # Driver code n = 3 k = 2 print(countWays(n, k)) # This code is contributed by shubhamsingh10 Output6 Time Complexity: O(N)Auxiliary Space: O (N) Painting Fence Algorithm in Python (Space Optimized): We can optimize the above solution by use one variable instead of a table. Below is the implementation of the problem: Python # Python3 program for Painting # Fence Algorithm # Returns count of ways to color k posts using k colors def countWays(n, k) : # There are k ways to color first post total = k mod = 1000000007 # There are 0 ways for single post to # violate (same color_ and k ways to # not violate (different color) same, diff = 0, k # Fill for 2 posts onwards for i in range(2, n + 1): # Current same is same as previous diff same = diff # We always have k-1 choices for next post diff = (total * ((k - 1) % mod)) % mod # Total choices till i. total = (same + diff) % mod return total # Driver code if __name__ == "__main__" : n, k = 3, 2 print(countWays(n, k)) # This code is contributed by @nibeditans Output6 Time Complexity: O(N)Auxiliary Space: O(1) Advertise with us Next Article Painting Fence Algorithm J jyotijb23 Follow Similar Reads Painting Fence Algorithm Given a fence with n posts and k colors, the task is to find out the number of ways of painting the fence so that not more than two consecutive posts have the same color.Examples:Input: n = 2, k = 4Output: 16Explanation: We have 4 colors and 2 posts.Ways when both posts have same color: 4 Ways when 15 min read Graph Coloring Algorithm in Python Given an undirected graph represented by an adjacency matrix. The graph has n nodes, labeled from 1 to n. The task is to assign colors to each node in such a way that no two adjacent nodes have the same color. The challenge is to solve this problem using the minimum number of colors. Graph Coloring 8 min read Mid-Point Circle Drawing Algorithm The mid-point circle drawing algorithm is an algorithm used to determine the points needed for rasterizing a circle. We use the mid-point algorithm to calculate all the perimeter points of the circle in the first octant and then print them along with their mirror points in the other octants. This wi 15+ min read Painter's Algorithm in Computer Graphics Painter's algorithm is the algorithm which is introduced by Hewells in 1972. The techniques used by these algorithms are image space and object space. The name of this algorithm is Painter's because it's working is like a painter who creating an oil painting. Just like an artist paints, he start hi 4 min read Flood fill Algorithm - how to implement fill() in paint? Flood Fill is a classic algorithm used to change the color of an area in a 2D image where all pixels are connected and have the same initial color. Think of it like the paint bucket tool in graphic design software like MS Paint or Photoshopâwhen you click on a spot, it automatically fills that area 2 min read Sorting Algorithm Visualization : Merge Sort The human brain can easily process visuals instead of long codes to understand the algorithms. In this article, a program that program visualizes the Merge sort Algorithm has been implemented. The GUI(Graphical User Interface) is implemented using pygame package in python. Approach: An array of rand 3 min read Article Tags : Dynamic Programming DSA Python-DSA Practice Tags : Dynamic Programming Like