Algo-Phantoms / Algo-Tree

Algo-Tree is a collection of Algorithms and data structures which are fundamentals to efficient code and good software design. Creating and designing excellent algorithms is required for being an exemplary programmer. It contains solutions in various languages such as C++, Python and Java.
MIT License
357 stars 624 forks source link

Painting Fence problem #1973

Open Fatema110 opened 3 years ago

Fatema110 commented 3 years ago

Painting Fence problem

In a fence, with n posts and k available colors, we need to find out how many different ways we can paint the fence so that at most two adjacent posts have the same color. Output the answer modulo 10^9 + 7

How many posts are there on the fence? 3 How many colors are available? 7

The number of ways to color the given posts is 336

SAMPLE II How many posts are there on the fence? 5 How many colors are available? 0

No color is available to paint!!!

Time Complexity: O(n) where 'n' is the number of posts to paint Space Complexity: O(n)