首页 > 其他分享 >LeetCode 1411. Number of Ways to Paint N × 3 Grid

LeetCode 1411. Number of Ways to Paint N × 3 Grid

时间:2024-06-02 23:56:04浏览次数:12  
标签:int Ways Number long two Paint colors grid row

原题链接在这里:https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid/description/

题目:

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 109 + 7.

Example 1:

Input: n = 1
Output: 12
Explanation: There are 12 possible way to paint the grid as shown.

Example 2:

Input: n = 5000
Output: 30228214

Constraints:

  • n == grid.length
  • 1 <= n <= 5000

题解:

First calculate the possibilities of first row. Then keep adding the next row. The new row relies on previous row selections.

For the first row, there are 2 choices, one is 3 different colors or 2 different colors.

3 different colors have 6 arrangements. 123, 132, 213, 231, 312, 321

2 different colors have 6 arrangements. 121, 131, 212, 232, 313, 323

For the next row, 3 colors => two 3 colors + two 2 colors

2 colors => three 3 colors + two 2 colors.

Thus, 

long newC3 = (2 * c3 + 2 * c2) % M; long newC2 = (2 * c3 + 3 * c2) % M;

Time Complexity: O(n).

Space: O(1).

AC Java:

 1 class Solution {
 2     static int M = (int)(1e9 + 7);
 3     public int numOfWays(int n) {
 4         long c3 = 6;
 5         long c2 = 6;
 6         for(int i = 2; i <= n; i++){
 7             long newC3 = (2 * c3 + 2 * c2) % M;
 8             long newC2 = (2 * c3 + 3 * c2) % M;
 9             c3 = newC3;
10             c2 = newC2;
11         }
12 
13         return (int)((c2 + c3) % M);
14     }
15 }

类似Paint Fence.

标签:int,Ways,Number,long,two,Paint,colors,grid,row
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18227847

相关文章

  • CF 947 (Div. 1 + Div. 2) D. Paint the Tree
    时间:24_05_30原题:CodeforcesRound947(Div.1+Div.2)标签:二分/数据结构/[[DFS]]/[[模拟]]/[[树形结构]]题目大意有\(n\)个顶点的树,初始时每个节点都是白色树上有两个棋子,为\(P_A\)和\(P_B\),分别位于\(a\),\(b\)顶点\(P_A\)所在的顶点会被涂成红色,\(P_B\)......
  • [LeetCode] 1365. How Many Numbers Are Smaller Than the Current Number 有多少小于
    Giventhearray nums,foreach nums[i] findouthowmanynumbersinthearrayaresmallerthanit.Thatis,foreach nums[i] youhavetocountthenumberofvalid j's suchthat j!=i and nums[j]<nums[i].Returntheanswerinanarray.Example1......
  • spark sql中的FORMAT_NUMBER和ROUND函数
    一、例子:FORMAT_NUMBER(ROUND(value,2),'0.00')二、ROUND函数的作用:用于将数值字段舍入到指定的小数位数,如果未指定小数位数,则默认将数字舍入到最接近的整数。三、FORMAT_NUMBER函数的作用:用于将数字格式化为指定的格式,而不是进行舍入。四、两者的区别:如果小数点后面的数字,最......
  • 【YashanDB知识库】kettle从DM8的number类型同步到YashanDB的varchar类型,存入是科学计
    【标题】kettle从DM8的number类型同步到YashanDB的varchar类型,存入是科学计数法形式的数据【问题分类】数据导入导出【关键字】数据同步,number类型,科学计数法【问题描述】客户查询不到准确数据,只看到科学计数法展示的字符串。number类型存入到Oracle(MySQL)的varchar类型是正常......
  • 每日一题——Python实现PAT甲级1023 Have Fun with Numbers(举一反三+思想解读+逐步优
    一个认为一切根源都是“自己不够强”的INTJ个人主页:用哲学编程-CSDN博客专栏:每日一题——举一反三Python编程学习Python内置函数目录我的写法:​编辑代码点评:代码功能时间复杂度空间复杂度优化建议哲学和编程思想举一反三题目链接我的写法:num=input()digits_in......
  • P10298 [CCC 2024 S4] Painting Roads
    原题链接题解由易到难,先不考虑交替的事情,既然要尽量少的涂色,那么我最少要涂几条颜色的边?(由于图不一定联通,这里先考虑连通图的情况)如果一条边处于一个环内,那么这个边就可以不涂色。所以只要有环我就可以选择一条边不涂色,那么到最后,涂色的边构成一棵树接下来考虑这颗树能否实现......
  • user版本修改Build number
    在文件`packages/apps/Settings/src/com/android/settings/DeviceInfoSettings.java`中setStringSummary("build_number",Build.DISPLAY);指定了设置--关于设备--版本号。Build.DISPLAY即Build类中的DISPLAY变量,在文件frameworks/base/core/java/android/os/Build.java......
  • Number Theory(3)
    7数论函数基础数论函数是数论中相当重要的一环,我们先来将一些基本的函数。7.1相关定义数论函数:定义域为正整数的函数称为数论函数。因其在所有正整数处均有定义,故可视作数列。OI中常见的数论函数的陪域(即可能的取值范围)为整数。加性函数:若对于任意\(a,b\in\mathbb......
  • Number Theory(4)
    11莫比乌斯函数尝试按照《具体数学》的顺序引入莫比乌斯反演。11.1引入莫比乌斯函数\(\mu(m)\)是根据19世纪的数学家奥古斯特·莫比乌斯命名的,他还发现了著名的莫比乌斯带,\(\mu(m)\)对所有整数\(m\ge1\)由等式\[\sum_{d\midm}\mu(d)=[m=1]\]来定义。这个式......
  • Number Theory(5)
    12奇妙筛法已开工。12.1杜教筛12.2Min-25筛Min_25筛可以在\(\mathcalO\left(\frac{n^{\frac34}}{\logn}\right)\)的时间复杂度内解决一类积性函数前缀和问题。说形象点就是\(1s\)能跑\(10^{10}\),相当优秀。要求:\(f(p)\)是关于\(p\)的低阶多项式,......