Given n stairs and we have 2 colour yellow and green the task is that we have to paint given stairs by given colour with condition is that we cannot paints two yellow steps directly after each other.
Input : n = 1 Output : 2 A single stair can be colored either as green or yellow. Input : n = 3 Output : 5
Case 1: When we have 1 stair, we can paint either yellow or green.
Case 2: When we have 2 stairs, we can paint first stair by either yellow or green but for next stair we can only paint by green because we cannot paint two yellow steps directly after each other. So total cases are three YG, GG, GY.
Case 3: When we have 3 stairs then we can paint it by in 5 ways.
If we take a closer look, we can notice that it follows Fibonacci Series.
Time Complexity : O(n)
Extra Space : O(n)
We can solve this problem in O(Log n) time also using matrix exponentiation solution for n-th Fibonacci Number.
- Ways to paint N paintings such that adjacent paintings don't have same colors
- Number of ways to paint a tree of N nodes with K distinct colors with given conditions
- Ways to color a 3*N board using 4 colors
- Number of ways to form an array with distinct adjacent elements
- Maximum sum such that no two elements are adjacent
- Rearrange characters in a string such that no two adjacent are same
- Maximum sum of difference of adjacent elements
- Permutations of string such that no two vowels are adjacent
- Generate permutations with only adjacent swaps allowed
- Longest subsequence such that adjacent elements have at least one common digit
- Count non-adjacent subsets from numbers arranged in Circular fashion
- Arrange first N natural numbers such that absolute difference between all adjacent elements > 1
- Ways to represent a number as a sum of 1's and 2's
- Count of different ways to express N as the sum of 1, 3 and 4
- Different ways to represent N as sum of K non-zero integers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.