Ways To Tile A Floor | Practice | GeeksforGeeks

Given a floor of dimensions 2 x W and tiles of dimensions 2 x 1, the task is to find the number of ways the floor can be tiled. A tile can either be placed horizontally i.e as a 1 x 2 tile or vertically i.e as 2 x 1 tile. Print the answer modulo 109+7.
 
Example 1:
Input:
W = 3
Output:
3
Explanation:
We need 3 tiles to tile the board
of size  2 x 3. 
We can tile in following ways:
1) Place all 3 tiles vertically. 
2) Place first tile vertically.
and remaining 2 tiles horizontally.
3) Place first 2 tiles horizontally
and remaining tiles vertically.
Example 2:
Input:
W = 4
Output:
5
Explanation:
For a 2 x 4 board, there are 5 ways
1) All 4 vertical
2) All 4 horizontal
3) First 2 vertical, remaining
2 horizontal.
4) First 2 horizontal, remaining
2 vertical.
5) Corner 2 vertical, middle
2 horizontal.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfWays() which takes an Integer N as input and returns the answer.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 105
0
ganesh3567lokhande
This comment was deleted.
0
shreeshsingh2 months ago
# DP Space Optimized Approach

MOD = (int)(1e9+7)

class Solution:
    def numberOfWays(self, n: int):
        # code here
        
        if n <= 2:
			return n
            
        prevPrev = 1
        prev = 2
        res = 0
        for i in range(3, n+1):
            
            res = (prev + prevPrev) % MOD
            prevPrev = prev
            prev = res
            
            
        return res
0
mithileshkumarsingh2 months ago
  long long dp[100001];
  int mod=1e9+7;
  long long solve(long long  n){
      if(n<=2)return n;
      if(dp[n]!=-1)return dp[n];
      return  dp[n]=(solve(n-1)+solve(n-2))%mod;
0
acomplishedretard2 months ago
FOR THOSE WHO ARE TRYING TO USE MEMOIZATION AND RECURSION class Solution { public:    long long fib[100001] ={0,1,2,0} ;           long long numberOfWays(long long N)       {       //code here       //  if(N <= 2)       // {       //     return N ;       // }              if(fib[N] != 0)       {           return fib[N];       }                    fib[N] = (numberOfWays(N-1)+ numberOfWays(N-2))%1000000007;       return fib[N];   }};
0
blackwizard3 months ago
Can anyone help me in this code? Why is this not working? 
class Solution {
    static Long numberOfWays(int N) {
        // code here
        
        long dp[] = new long[N+1];
        dp[1] = 1;
        dp[2] = 2;
        
        for(int i = 3; i<=N; i++){
            dp[i] = (dp[i-1] + dp[i-2])%1000000007;
        }
        
        return dp[N];
    }
};
+3
bhaskarkumarpro6 months ago
ll mod=1e9+7;   long long numberOfWays(long long n) {       //code here       int dp[n+1]={0};       dp[1]=1;       dp[2]=2;              for(int i=3;i<=n;i++)           dp[i]=(dp[i-1]+dp[i-2])%mod;                  return dp[n];   }
0
Sagar Sikchi9 months ago
Sagar Sikchi
Java:Expected Time Complexity: O(N)Expected Auxiliary Space: O(1)
class Solution {    static Long numberOfWays(int N) {        long M = 1000000007, a = 1, b = 1, c = 1;        for(int i = 2; i <= N; i++) {            c = (a + b) % M;            a = b;            b = c;        }        return c;    }};
0
Anshiv Gagneja9 months ago
Anshiv Gagneja
Why %1000000007 is required
0
Nalin Nishant10 months ago
Nalin Nishant
long long numberOfWays(long long n) {        long long dp[n + 1];                dp[0] = 1;        dp[1] = 1;                for(int i = 2; i <= n; i++){            dp[i] = (dp[i-1] + dp[i-2])%1000000007;        }                return dp[n];    }
0
Dakshendra Singh10 months ago
Dakshendra Singh
class Solution{public:    long long numberOfWays(long long n) {        //code here        long long int arr[n+1];        arr[0]=1;        arr[1]=1;        for(long long int i=2;i<=n;i++){            arr[i]=(arr[i-1]+arr[i-2])%(1000000007);        }        return arr[n];    }};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.