Java实现 LeetCode 790 多米诺和托米诺平铺(递推)
790. 多米诺和托米诺平铺
有两种形状的瓷砖:一种是 2x1 的多米诺形,另一种是形如 “L” 的托米诺形。两种形状都可以旋转。
XX <- 多米诺
XX <- “L” 托米诺
X
给定 N 的值,有多少种方法可以平铺 2 x N 的面板?返回值 mod 10^9 + 7。
(平铺指的是每个正方形都必须有瓷砖覆盖。两个平铺不同,当且仅当面板上有四个方向上的相邻单元中的两个,使得恰好有一个平铺有一个瓷砖占据两个正方形。)
示例:
输入: 3
输出: 5
解释:
下面列出了五种不同的方法,不同字母代表不同瓷砖:
XYZ XXZ XYY XXY XYY
XYZ YYZ XZZ XYY XXY
提示:
N 的范围是 [1, 1000]
class Solution {
//dp[i][0]是第n行,并且是平铺
//dp[i][1]是第n行,不平铺的
// public int numTilings(int N) {
// long[][] dp = new long[N+1][3];
// dp[0][0] = 1;
// dp[0][1] = 0;
// int MOD = 1000000007;
// for(int i = 1 ; i <= N ; ++i){
// long temp = i < 2 ? 0 : dp[i -2][0];
// dp[i][0] = (temp + dp[i-1][0] + 2 * dp[i-1][1]) % MOD;
// dp[i][1] = (temp +dp[i-1][1]) % MOD;
// }
// return (int)dp[N][0];
// }
public int numTilings(int N) {
int mod = 1000000007;
int[] dp = new int[N+3];
dp[0] = 1;
dp[1] = 1;
dp[2] = 2;
dp[3] = 5;
for(int i = 4; i <= N; i++){
//这里全是平铺的,
//我当前这一位,可以是我上一位平铺的+一个2*1的,
//我可以把1*2的放到最上面,也可以放在最下面,是两种可能,所以*2
//还可以是我三位前的那个,因为可以是两个L
//但这里,我开头或者结尾可能是L的,如果我们在那个基础上加上L
//可能会导致重复,以至于要/2,也就变成了三位前的那个*2/2==1
dp[i] = (2*(dp[i-1] % mod) % mod + dp[i-3] % mod) % mod;
}
return dp[N] % mod;
}
}
Java实现 LeetCode 790 多米诺和托米诺平铺(递推)的更多相关文章
- [Swift]LeetCode790. 多米诺和托米诺平铺 | Domino and Tromino Tiling
We have two types of tiles: a 2x1 domino shape, and an "L" tromino shape. These shapes may ...
- java中对于二位数组的简单操作,进而可以递推复杂操作
1.程序 2.结果 3.内循环的次数 arr[x].length
- BZOJ 1019 :[SHOI2008]汉诺塔(递推)
好吧蒟蒻还是看题解的 其实看到汉诺塔就该想到是递推了 设f[i][j]表示i个在j杆转移到另一个杆的次数 g[i][j]表示i个在j杆转移到那个杆上 可得 f[i][j]=f[i-1][j]+1+f[ ...
- Java实现 LeetCode 838 推多米诺(暴力模拟)
838. 推多米诺 一行中有 N 张多米诺骨牌,我们将每张多米诺骨牌垂直竖立. 在开始时,我们同时把一些多米诺骨牌向左或向右推. 每过一秒,倒向左边的多米诺骨牌会推动其左侧相邻的多米诺骨牌. 同样地, ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
随机推荐
- 【Kafka】Producer API
Producer API Kafka官网文档给了基本格式 地址:http://kafka.apachecn.org/10/javadoc/index.html?org/apache/kafka/cli ...
- Quartus II 与modelsim连接不上的问题
在Quartus II 中tools>options>General>EDA Tool Options 设置modelsim 路径 说明:不管是Quartus II 与modelsi ...
- 设计模式之GOF23模板模式
模板模式template method 场景:具有具体流程,但具体某一步的业务不同 到银行办理业务:排队取号,办理业务,给员工打分 请客吃饭:等待,点单,吃饭,结账 模板方法模式介绍:模板方法是编程常 ...
- layui的layer.msg,form提交
layer.msg('提示文字',{shift:1,time:2000},function(){ location.reload();});其中shift是提示框的动画样式,-1为无动画form.on ...
- Crash-fix-2:org.springframework.http.converter.HttpMessageNotReadableException
最近开始对APP上的Crash进行对应,发现有好多常见的问题,同一个问题在多个APP都类似的出现了,这里记录下这些常见的错误. crash Log: org.springframework.http. ...
- 比较接口(Comparable ,Comparator)及流的聚合操作
Comparable 及 Comparator 的区别 Comparable 一个内比较器,类可以和自己比较 compareTo() 自然比较方法 public interface Comparabl ...
- Azure B2C登录,react-web端实现,自定义登录页面ui
import React, { Component } from 'react'; import Particles from 'react-particles-js'; import { Form, ...
- ubuntu下图形程序自启动的几种方法
版权声明:本文为本文为博主原创文章,转载请注明出处.如有问题,欢迎指正.博客地址:https://www.cnblogs.com/wsg1100/ @ 目录 0.前言 1.带桌面环境的自动启动 1.1 ...
- wepy+vant-weapp踩坑记
最近用了几个月的wepy框架,碰到了挺多问题,这里总结一下 1.clone的代码无法再本地运行,wepy报错 解决方案: 执行命令 : `npm install wepy-cli@1.6.1-alph ...
- flask之Flask、config配置
flask_config.py ''' flask的配置: 1.flask项目初始化配置: (1)app=Flask(__name__)#初始化声明falsk项目为当前py文件,app应用变量名可以更 ...