Leetcode 790: Domino and Tromino Tiling

grid47
grid47
Exploring patterns and algorithms
Aug 20, 2024 6 min read

A grid being tiled with dominos and trominos, with the valid tiles glowing softly as they’re placed.
Solution to LeetCode 790: Domino and Tromino Tiling Problem

You are given a 2 x n grid and two types of tiles: a 2 x 1 domino and a tromino (which can be rotated). You must cover the grid entirely with these tiles. The task is to find the number of possible ways to tile the 2 x n board, modulo 10^9 + 7. Each tile must cover exactly two adjacent squares, and every square must be covered by a tile.
Problem
Approach
Steps
Complexity
Input: You are given a single integer n, representing the length of the grid.
Example: n = 4
Constraints:
• 1 <= n <= 1000
Output: Return the number of ways to tile the 2 x n grid, modulo 10^9 + 7.
Example: Output: 11
Constraints:
• The output should be returned as a single integer.
Goal: To find the number of ways to tile a 2 x n grid with dominos and trominos, use dynamic programming to compute the number of ways for each grid size up to n.
Steps:
• Initialize a dynamic programming array where dp[i] stores the number of ways to tile a 2 x i grid.
• Base cases: dp[1] = 1, dp[2] = 2, dp[3] = 5.
• For each i from 4 to n, calculate dp[i] as (2 * dp[i-1] + dp[i-3]) % mod.
• Return dp[n].
Goal: Ensure that the value of n is within the specified range, i.e., 1 <= n <= 1000.
Steps:
• The value of n is between 1 and 1000 inclusive.
Assumptions:
• The input n is always valid, and there are no constraints on the arrangement of the tiles other than that they must fill the grid completely.
Input: Input: n = 3
Explanation: The number of ways to tile a 2 x 3 grid is 5. The five different ways are achieved by combining 2 x 1 dominoes and 2 x 2 trominos in different configurations.

Input: Input: n = 1
Explanation: There is only one way to tile a 2 x 1 grid, using one 2 x 1 domino. Hence, the output is 1.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus