06. Ways To Tile A Floor
β GFG solution to the Ways to Tile a Floor problem: compute the number of ways to fill a 2Γn board using 2Γ1 tiles via dynamic programming or Fibonacci logic. π
The problem can be found at the following link: π Question Link
π§© Problem Description
Given a floor of dimensions 2 Γ n and tiles of dimensions 2 Γ 1, the task is to find the number of ways the floor can be tiled. A tile can either be placed:
Horizontally (as 1 Γ 2 tile)
Vertically (as 2 Γ 1 tile)
Note: Two tiling arrangements are considered different if the placement of at least one tile differs.
π Examples
Example 1
Input: n = 3
Output: 3
Explanation: We need 3 tiles to tile the board of size 2 Γ 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: n = 4
Output: 5
Explanation: We need 4 tiles to tile the board of size 2 Γ 4.
We can tile in following 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.π Constraints
$1 \le n \le 45$
β
My Approach
This problem is a classic Dynamic Programming problem that follows the Fibonacci sequence pattern. The key insight is understanding the recurrence relation:
Iterative Dynamic Programming
Base Cases:
If
n = 1: Only 1 way (place one vertical tile).If
n = 2: 2 ways (either two vertical tiles or two horizontal tiles stacked).
Recurrence Relation:
For position
n, we have two choices:Place a vertical tile at position
n, leavingn-1positions:ways(n-1)Place two horizontal tiles at position
n, leavingn-2positions:ways(n-2)
Therefore:
ways(n) = ways(n-1) + ways(n-2)
Space Optimization:
Instead of maintaining entire DP array, use two variables to track previous two states.
This reduces space complexity from O(n) to O(1).
Iterative Computation:
Initialize
a = 1(for n=1) andb = 1(for n=0, representing empty floor).Iterate from position 2 to n, updating:
new_value = a + b.Shift variables:
a = b,b = new_value.
Result:
After n iterations,
bcontains the answer.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), as we iterate through n positions once, performing constant time operations at each step.
Expected Auxiliary Space Complexity: O(1), as we only use two variables to store previous states regardless of the input size.
π§βπ» Code (C++)
class Solution {
public:
int numberOfWays(int n) {
if (n <= 1) return 1;
int a = 1, b = 1;
while (n-- > 1) {
int c = a + b;
a = b;
b = c;
}
return b;
}
};β Code (Java)
class Solution {
public int numberOfWays(int n) {
if (n <= 1) return 1;
int a = 1, b = 1;
while (n-- > 1) {
int c = a + b;
a = b;
b = c;
}
return b;
}
}π Code (Python)
class Solution:
def numberOfWays(self, n):
if n <= 1: return 1
a, b = 1, 1
for _ in range(n - 1):
a, b = b, a + b
return bπ§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated