Skip to content

Commit 919ab15

Browse files
chore: add LeetCode daily solution
1 parent 1957305 commit 919ab15

5 files changed

Lines changed: 88 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Number of Ways to Paint N × 3 Grid (Hard)
2+
3+
**Problem ID:** 1411
4+
**Date:** 2026-01-03
5+
**Link:** https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid/
6+
7+
## Approach
8+
9+
To solve the problem of counting the number of ways to paint an n × 3 grid with the constraint that no two adjacent cells share the same color, we can use a dynamic programming approach.
10+
11+
### Main Idea:
12+
The core idea is to define a state that captures the valid configurations of painting the grid rows while adhering to the coloring constraints. We can represent the configurations of the last row using a state that reflects the colors of the three cells in that row.
13+
14+
### State Representation:
15+
1. **State Definition**: Let `dp[i]` represent the number of ways to paint the first `i` rows of the grid.
16+
2. **Transition**: To compute `dp[i]`, we consider the valid colorings of the `i-th` row based on the configurations of the `(i-1)-th` row.
17+
- If we denote the number of ways to paint the last row with a certain configuration, we can determine the number of valid configurations for the next row.
18+
- For each configuration of the last row, there are specific ways to paint the next row that respect the adjacency rule.
19+
20+
### Recurrence Relation:
21+
- The number of ways to paint a row can be derived from the previous row. For each cell in the current row, we have 3 choices of colors, but we must ensure that adjacent cells do not share the same color.
22+
- The transitions can be calculated based on:
23+
- The number of ways to paint the previous row (with no restrictions on the same color).
24+
- The restrictions imposed by the colors chosen in the previous row.
25+
26+
### Base Case:
27+
- For `n = 1`, we can directly calculate the number of ways to paint a single row of three cells, which is `12` (since there are 3 choices for the first cell and 2 choices for the second and third cells).
28+
29+
### Complexity:
30+
- The time complexity of this approach is O(n) since we compute each `dp[i]` based on the previous state, and space complexity can be reduced to O(1) by only storing the last two states (current and previous).
31+
32+
### Final Calculation:
33+
- The final answer will be `dp[n]`, which gives the number of valid ways to paint the entire grid modulo \(10^9 + 7\).
34+
35+
In summary, the problem can be efficiently solved using dynamic programming by defining a state that captures the valid configurations of the last row and using recurrence relations to build up to the solution for `n` rows.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int numOfWays(int n) {
3+
final int MOD = 1000000007;
4+
long same = 6; // Ways to paint two adjacent cells the same color
5+
long diff = 6; // Ways to paint two adjacent cells different colors
6+
long total = (same + diff) % MOD;
7+
8+
for (int i = 2; i <= n; i++) {
9+
long newSame = (diff * 2) % MOD;
10+
long newDiff = (total * 3) % MOD;
11+
same = newSame;
12+
diff = newDiff;
13+
total = (same + diff) % MOD;
14+
}
15+
16+
return (int) total;
17+
}
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var numOfWays = function(n) {
2+
const MOD = 1e9 + 7;
3+
let same = 6; // Ways to paint two adjacent cells the same color
4+
let diff = 6; // Ways to paint two adjacent cells different colors
5+
6+
for (let i = 2; i <= n; i++) {
7+
let newSame = (diff * 2) % MOD;
8+
let newDiff = (same + diff * 2) % MOD;
9+
same = newSame;
10+
diff = newDiff;
11+
}
12+
13+
return (same + diff) % MOD;
14+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def numOfWays(self, n: int) -> int:
3+
MOD = 10**9 + 7
4+
5+
if n == 1:
6+
return 12
7+
8+
# dp[i] represents the number of ways to paint the grid of size i
9+
dp = [0] * (n + 1)
10+
dp[1] = 12
11+
12+
for i in range(2, n + 1):
13+
dp[i] = (dp[i - 1] * 12 - dp[i - 2] * 6) % MOD
14+
15+
return dp[n]
16+
17+
# Example usage:
18+
# sol = Solution()
19+
# print(sol.numOfWays(1)) # Output: 12
20+
# print(sol.numOfWays(5000)) # Output: 30228214

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
324324
- 2025-12-31 — [Last Day Where You Can Still Cross](https://leetcode.com/problems/last-day-where-you-can-still-cross/) (Hard) → `Hard/2025-12-31-1970-Last-Day-Where-You-Can-Still-Cross`
325325
- 2026-01-01 — [Plus One](https://leetcode.com/problems/plus-one/) (Easy) → `Easy/2026-01-01-66-Plus-One`
326326
- 2026-01-02 — [N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/) (Easy) → `Easy/2026-01-02-961-N-Repeated-Element-in-Size-2N-Array`
327+
- 2026-01-03 — [Number of Ways to Paint N × 3 Grid](https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid/) (Hard) → `Hard/2026-01-03-1411-Number-of-Ways-to-Paint-N-3-Grid`

0 commit comments

Comments
 (0)