Advertisement

Number of Ways to Paint N × 3 Grid - LeetCode 1411 Solution

Number of Ways to Paint N × 3 Grid - Complete Solution Guide

Number of Ways to Paint N × 3 Grid is LeetCode problem 1411, a Hard 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 a grid of size n x 3 and you want to paint each cell of the grid with exactly one of the three colors: Red , Yellow, or Green while making sure that no two adjacent cells have the same color (i.e., no two cells that share vertical or horizontal sides have the same color). Given n the number of rows of the grid, return the number of ways you can paint this grid . As the answer may grow large, the answer must be computed modulo 10 9 + 7 . Example 1: Input: n = 1 Output: 12 Explanation: Th

Detailed Explanation

The problem asks us to find the number of ways to color an n x 3 grid using three colors (Red, Yellow, Green) such that no two adjacent cells (horizontally or vertically) have the same color. We need to return the result modulo 10^9 + 7 to handle potentially large numbers. The input is the number of rows, 'n', and the output is the number of valid colorings modulo 10^9 + 7. The constraint is 1 <= n <= 5000.

Solution Approach

The solution employs dynamic programming to efficiently calculate the number of valid grid colorings. It leverages the observation that the number of ways to color a row depends only on the previous row. We maintain two variables: 'aba' and 'abc', representing the number of valid colorings for a row with patterns 'aba' and 'abc', respectively. We initialize these variables for the first row (n=1). Then, we iterate from row 2 to 'n', updating 'aba' and 'abc' based on the number of ways they can be formed from the previous row's 'aba' and 'abc' configurations. Finally, we return the sum of 'aba' and 'abc' modulo 10^9 + 7.

Step-by-Step Algorithm

  1. Step 1: Initialize 'MOD' to 10^9 + 7. This constant will be used for modulo operations to prevent integer overflow.
  2. Step 2: Initialize 'aba' and 'abc' to 6. For n=1, there are 6 'aba' patterns (e.g., RGR, RBR, GRG, GBG, YRY, YBY) and 6 'abc' patterns (e.g., RGB, RBG, GRB, GBR, YRB, YBR).
  3. Step 3: Iterate from 2 to 'n' (exclusive). In each iteration, calculate the new values for 'aba' and 'abc' based on the previous values.
  4. Step 4: The number of ways to form an 'aba' pattern in the current row is (aba * 3 + abc * 2) % MOD, using the previous row's 'aba' and 'abc' counts.
  5. Step 5: The number of ways to form an 'abc' pattern in the current row is (aba * 2 + abc * 2) % MOD, using the previous row's 'aba' and 'abc' counts.
  6. Step 6: Update 'aba' and 'abc' with the newly calculated values for the current row.
  7. Step 7: After the loop finishes, return (aba + abc) % MOD. This is the total number of ways to paint the n x 3 grid.

Key Insights

  • Insight 1: The problem can be solved using dynamic programming. We don't need to explicitly explore all color combinations because the number of ways to color row 'i' depends only on row 'i-1'.
  • Insight 2: We can categorize each row into two types: 'aba' (where the first and third colors are the same) and 'abc' (where all three colors are different).
  • Insight 3: Instead of storing the entire grid, we only need to keep track of the number of ways to color the last row as 'aba' and 'abc'. This reduces the space complexity significantly.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Dynamic Programming.

Companies

Asked at: Fortinet.