1411. Number of Ways to Paint N × 3 Grid
/**
* Problem: LeetCode 1411 - Number of Ways to Paint N × 3 Grid
* Difficulty: Hard
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int numOfWays(int n) {
long color2 = 6; // Patterns with 2 distinct colors (e.g., ABA)
long color3 = 6; // Patterns with 3 distinct colors (e.g., ABC)
long mod = 1_000_000_007L;
for (int i = 2; i <= n; i++) {
long nextColor2 = (3 * color2 + 2 * color3) % mod;
long nextColor3 = (2 * color2 + 2 * color3) % mod;
color2 = nextColor2;
color3 = nextColor3;
}
return (int) ((color2 + color3) % mod);
}
}
"""
Problem: LeetCode 1411 - Number of Ways to Paint N × 3 Grid
Difficulty: Hard
Language: Python
Author: CodeByTushu
"""
class Solution:
def numOfWays(self, n: int) -> int:
MOD = 1_000_000_007
color2 = 6 # Patterns with 2 distinct colors (e.g., ABA)
color3 = 6 # Patterns with 3 distinct colors (e.g., ABC)
for _ in range(2, n + 1):
next_color2 = (3 * color2 + 2 * color3) % MOD
next_color3 = (2 * color2 + 2 * color3) % MOD
color2, color3 = next_color2, next_color3
return (color2 + color3) % MOD
/**
* Problem: LeetCode 1411 - Number of Ways to Paint N × 3 Grid
* Difficulty: Hard
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
int numOfWays(int n) {
long long color2 = 6; // Patterns with 2 distinct colors (e.g., ABA)
long long color3 = 6; // Patterns with 3 distinct colors (e.g., ABC)
const int MOD = 1e9 + 7;
for (int i = 2; i <= n; i++) {
long long nextColor2 = (3 * color2 + 2 * color3) % MOD;
long long nextColor3 = (2 * color2 + 2 * color3) % MOD;
color2 = nextColor2;
color3 = nextColor3;
}
return (color2 + color3) % MOD;
}
};
/**
* Problem: LeetCode 1411 - Number of Ways to Paint N × 3 Grid
* Difficulty: Hard
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* @param {number} n
* @return {number}
*/
var numOfWays = function(n) {
const MOD = 1000000007n;
let color2 = 6n; // Patterns with 2 distinct colors (e.g., ABA)
let color3 = 6n; // Patterns with 3 distinct colors (e.g., ABC)
for (let i = 2; i <= n; i++) {
const nextColor2 = (3n * color2 + 2n * color3) % MOD;
const nextColor3 = (2n * color2 + 2n * color3) % MOD;
color2 = nextColor2;
color3 = nextColor3;
}
return Number((color2 + color3) % MOD);
};