Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8a4ab08

Browse files
committed
Add solution #1411
1 parent ff70a13 commit 8a4ab08

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,287 LeetCode solutions in JavaScript
1+
# 1,288 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1078,6 +1078,7 @@
10781078
1408|[String Matching in an Array](./solutions/1408-string-matching-in-an-array.js)|Easy|
10791079
1409|[Queries on a Permutation With Key](./solutions/1409-queries-on-a-permutation-with-key.js)|Medium|
10801080
1410|[HTML Entity Parser](./solutions/1410-html-entity-parser.js)|Medium|
1081+
1411|[Number of Ways to Paint N × 3 Grid](./solutions/1411-number-of-ways-to-paint-n-3-grid.js)|Hard|
10811082
1415|[The k-th Lexicographical String of All Happy Strings of Length n](./solutions/1415-the-k-th-lexicographical-string-of-all-happy-strings-of-length-n.js)|Medium|
10821083
1422|[Maximum Score After Splitting a String](./solutions/1422-maximum-score-after-splitting-a-string.js)|Easy|
10831084
1431|[Kids With the Greatest Number of Candies](./solutions/1431-kids-with-the-greatest-number-of-candies.js)|Easy|
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 1411. Number of Ways to Paint N × 3 Grid
3+
* https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid/
4+
* Difficulty: Hard
5+
*
6+
* You have a grid of size n x 3 and you want to paint each cell of the grid with exactly one of
7+
* the three colors: Red, Yellow, or Green while making sure that no two adjacent cells have the
8+
* same color (i.e., no two cells that share vertical or horizontal sides have the same color).
9+
*
10+
* Given n the number of rows of the grid, return the number of ways you can paint this grid.
11+
* As the answer may grow large, the answer must be computed modulo 109 + 7.
12+
*/
13+
14+
/**
15+
* @param {number} n
16+
* @return {number}
17+
*/
18+
var numOfWays = function(n) {
19+
const MOD = 1000000007n;
20+
let pattern121 = 6n;
21+
let pattern123 = 6n;
22+
23+
for (let row = 2; row <= n; row++) {
24+
const nextPattern121 = (3n * pattern121 + 2n * pattern123) % MOD;
25+
const nextPattern123 = (2n * pattern121 + 2n * pattern123) % MOD;
26+
pattern121 = nextPattern121;
27+
pattern123 = nextPattern123;
28+
}
29+
30+
return Number((pattern121 + pattern123) % MOD);
31+
};

0 commit comments

Comments
 (0)