VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-number-ways-tile-floor-size-n-x-m-using-1-x-m-size-tiles/

⇱ Count ways to tile the floor of size n x m using 1 x m size tiles - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count ways to tile the floor of size n x m using 1 x m size tiles

Last Updated : 31 May, 2026

Given a floor of size n x m and tiles of size 1 x m. The problem is to count the number of ways to tile the given floor using 1 x m tiles. A tile can either be placed horizontally or vertically. 
Both n and m are positive integers and 2 < = m.

Examples: 

Input : n = 2, m = 3
Output: 1
Explanation: Only one combination to place two tiles of size 1 x 3 horizontally on the floor of size 2 x 3.

Input : n = 4, m = 4
Output: 2
Explanation: There are two ways to tile the given floor. One way is to place 1 x 4 size of tile vertically and another one is to place them horizontally.

[Naive] Recursively Trying Horizontal and Vertical - O(2ⁿ) Time and O(n) Space

The idea is to recursively place tiles in all possible valid ways. At any step, there are two choices: place one tile horizontally, which reduces the remaining floor length by 1, or place a tile vertically, which occupies m units of length and reduces the remaining floor length by m. The total number of tilings is the sum of the ways obtained from these two choices.

  • If n < m, only horizontal placement is possible → return 1
  • If n == m, two arrangements are possible: all horizontal and all vertical
  • Recursively compute: ways after placing one tile horizontally, countWays(n - 1, m) and ways after placing tiles vertically, countWays(n - m, m)
  • Return the sum of both values

Output
5

[Better] Using Memoization (Top Down DP) - O(n) Time and O(n) Space

The idea is to optimize the recursive solution by storing the results of previously solved subproblems. For a floor of length n, the number of ways depends on the answers for lengths n-1 and n-m. Since the same values are computed repeatedly in recursion, a DP array is used to memoize the results. Whenever a state is encountered again, the stored value is returned directly.

  • Create a DP array initialized with -1
  • If n < m, return 1
  • If n == m, return 2
  • If dp[n] is already computed, return it
  • Recursively compute: countWays(n - 1, m) and countWays(n - m, m)
  • Store their sum in dp[n] and return it
  • Apply modulo 10⁹ + 7 to avoid overflow

Output
5

[Expected] Using Bottom Up DP - O(n) Time and O(n) Space

The idea is to build the solution iteratively from smaller floor lengths to larger ones. Let dp[i] represent the number of ways to tile a floor of length i. For every length i, there are two possibilities: place a tile horizontally, contributing dp[i-1] ways, or place tiles vertically, contributing dp[i-m] ways.

  • Create a DP array of size n + 1
  • Initialize: dp[i] = 1 when i < m and dp[i] = 2 when i == m
  • For i > m: dp[i] = dp[i-1] + dp[i-m]
  • Apply modulo 10⁹ + 7 after each computation
  • Return dp[n]

Output
5


Comment
Article Tags: