Advertisement

Domino and Tromino Tiling - LeetCode 790 Solution

Domino and Tromino Tiling - Complete Solution Guide

Domino and Tromino Tiling is LeetCode problem 790, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You have two types of tiles: a 2 x 1 domino shape and a tromino shape. You may rotate these shapes. Given an integer n, return the number of ways to tile an 2 x n board . Since the answer may be very large, return it modulo 10 9 + 7 . In a tiling, every square must be covered by a tile. Two tilings are different if and only if there are two 4-directionally adjacent cells on the board such that exactly one of the tilings has both squares occupied by a tile. Example 1: Input: n = 3 Output: 5 Expla

Detailed Explanation

The problem asks us to calculate the number of ways to tile a 2 x n board using 2 x 1 dominoes and trominoes (L-shaped tiles). We need to return the answer modulo 10^9 + 7 because the number of ways can be very large. Two tilings are considered different if there are two adjacent cells where only one of the tilings has both squares occupied by a tile.

Solution Approach

The solution uses dynamic programming to calculate the number of ways to tile the 2 x n board. Instead of using a full array to store all intermediate results, it keeps track of only the last three values, d0, d1, and d2, which represent the number of ways to tile a 2 x (n-3), 2 x (n-2) and 2 x (n-1) board, respectively. It iterates from 3 to n, calculating the number of ways to tile a 2 x i board based on the previously calculated values using the recurrence relation: dp[i] = (2 * dp[i-1] + dp[i-3]) % MOD. The variables d0, d1, and d2 are updated iteratively to maintain the last three values.

Step-by-Step Algorithm

  1. Step 1: Initialize variables d0, d1, and d2 to 1, 1, and 2 respectively. These correspond to the number of ways to tile a 2x0, 2x1 and 2x2 board.
  2. Step 2: Handle the base cases where n is less than or equal to 2. In these cases, return n directly as the answer.
  3. Step 3: Iterate from 3 to n. In each iteration:
  4. Step 4: Calculate the current number of ways (current) as (2 * d2 + d0) % MOD. This follows from the recurrence relation derived from the possible ways to add tiles to the previous configurations.
  5. Step 5: Update d0, d1, and d2 to d1, d2, and current respectively to maintain the last three values.
  6. Step 6: After the loop finishes, return d2, which represents the number of ways to tile a 2 x n board.

Key Insights

  • Insight 1: The number of ways to tile the board depends on the number of columns (n). We can define a recursive relationship to represent this.
  • Insight 2: Dynamic programming is suitable because the problem has overlapping subproblems, meaning the number of ways to tile a smaller board contributes to the number of ways to tile a larger board.
  • Insight 3: We only need to store the results for the last three values of 'n' to compute the current value, allowing us to optimize space complexity to O(1).

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Dynamic Programming.

Companies

Asked at: WinZO.