Skip to content

Practice Painting a Grid With Three Different Colors

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1931
LeetCode Problem

Painting a Grid With Three Different Colors

Problem Statement

You are given two integers m and n. Consider an m x n grid where each cell is initially white. You can paint each cell red, green, or blue. All cells must be painted. Return the number of ways to color the grid with no two adjacent cells having the same color. Since the answer can be very large, return it modulo 10^9 + 7.

Source: Painting a Grid With Three Different Colors on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
m = 1, n = 1
Output
3

# Example 2

Input
m = 1, n = 2
Output
6

# Example 3

Input
m = 5, n = 5
Output
580986

Constraints

1 <= m <= 5
1 <= n <= 1000

Related LeetCode Problems

Practice more problems similar to Painting a Grid With Three Different Colors.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.