LC 756. Pyramid Transition Matrix
We are stacking blocks to form a pyramid. Each block has a color which is a one letter string, like `'Z'`.
For every block of color `C` we place not in the bottom row, we are placing it on top of a left block of color `A` and right block of color `B`. We are allowed to place the block there only if `(A, B, C)` is an allowed triple.
We start with a bottom row of bottom, represented as a single string. We also start with a list of allowed triples allowed. Each allowed triple is represented as a string of length 3.
Return true if we can build the pyramid all the way to the top, otherwise false.
Example 1:
Input: bottom = "XYZ", allowed = ["XYD", "YZE", "DEA", "FFF"]
Output: true
Explanation:
We can stack the pyramid like this:
A
/ \
D E
/ \ / \
X Y Z This works because ('X', 'Y', 'D'), ('Y', 'Z', 'E'), and ('D', 'E', 'A') are allowed triples.
Example 2:
Input: bottom = "XXYX", allowed = ["XXX", "XXY", "XYX", "XYY", "YXZ"]
Output: false
Explanation:
We can't stack the pyramid to the top.
Note that there could be allowed triples (A, B, C) and (A, B, D) with C != D.
Note:
bottomwill be a string with length in range[2, 8].allowedwill have length in range[0, 200].- Letters in all strings will be chosen from the set
{'A', 'B', 'C', 'D', 'E', 'F', 'G'}.
map + backtracing
Runtime: 9 ms, faster than 80.22% of Java online submissions for Pyramid Transition Matrix.
class Solution {
private Map<String, List<Character>> mp = new HashMap<>();
public boolean pyramidTransition(String bottom, List<String> allowed) {
for(int i=; i<allowed.size(); i++){
String tmp = allowed.get(i).substring(,);
if(mp.containsKey(tmp)){
mp.get(tmp).add(allowed.get(i).charAt(allowed.get(i).length()-));
}else{
mp.put(tmp, new ArrayList<>());
mp.get(tmp).add(allowed.get(i).charAt(allowed.get(i).length()-));
}
}
// for(String s : mp.keySet()){
// System.out.print(s + " " + mp.get(s) + "\n");
// }
return helper(bottom, "", );
}
public boolean helper(String bottom, String up, int index){
if(bottom.length() == ){
return true;
}else {
String tmp = bottom.substring(index, index+);
if(!mp.containsKey(tmp)) return false;
List<Character> clist = mp.get(tmp);
if(index+ == bottom.length()){
for(char x : clist){
String finalup = up + String.valueOf(x);
if(helper(finalup, "", )) return true;
}
}else {
for(char x : clist){
if(helper(bottom, up+String.valueOf(x), index+)) return true;
}
}
}
return false;
}
}
LC 756. Pyramid Transition Matrix的更多相关文章
- 【leetcode】756. Pyramid Transition Matrix
题目如下: We are stacking blocks to form a pyramid. Each block has a color which is a one letter string, ...
- 【LeetCode】756. Pyramid Transition Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- [LeetCode] Pyramid Transition Matrix 金字塔转变矩阵
We are stacking blocks to form a pyramid. Each block has a color which is a one letter string, like ...
- [Swift]LeetCode756. 金字塔转换矩阵 | Pyramid Transition Matrix
We are stacking blocks to form a pyramid. Each block has a color which is a one letter string, like ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- 算法与数据结构基础 - 深度优先搜索(DFS)
DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...
- leetcode 学习心得 (4)
645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- LeetCode All in One 题目讲解汇总(转...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...
随机推荐
- JDBC连接数据库报错:Loading class `com.mysql.jdbc.Driver'. This is deprecated.
使用JDBC连接数据库时出现报错, 报错内容:Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver cla ...
- 构建之法第二次作业【使用git和Vs实现四则运算】
[相关信息] Q A GIT地址 git地址 GIT用户名 Lin-000 学号后五位 62501 博客地址 博客地址 作业链接 此次作业链接 1.项目需求 程序接收一个命令行参数 n,然后随机产生 ...
- linux—-远程连接——管理工具
1,linux服务器需要安装ssh服务端,端口一般22 2,使用ssh客户端连接linux服务器: 常用工具: putty xshell 3,上传文件工具: 使用sftp,linux需要安装sftp服 ...
- Pycharm建立web2py项目
web2py是一种免费的,开源的web开发框架,用于敏捷地开发安全的,数据库驱动的web应用:web2p采用Python语言编写,并且可以使用Python编程.web2py是一个完整的堆栈框架,也就是 ...
- GNU Radio下QT GUI Tab Widget的使用方法
期望显示出的效果: 即将要显示的图放在各自的标签页中. 整体框图: 具体设置: QT GUI Tab Widget的设置: 其中 ID改为自己想改的,这里我写的是display GUI Hint所代表 ...
- 本地安装mysql脚本
[root@tianyun ~]# vim mysql_install.sh #!/usr/bin/env bash #mysql install 2 #by tianyun #yum 配置yum源 ...
- ACM-ICPC 2016 沈阳赛区现场赛 I. The Elder && HDU 5956(斜率DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5956 题意:一颗树上每条边有个权值,每个节点都有新闻要送到根节点就是1节点,运送过程中如果不换青蛙就是 ...
- 4、docker镜像:花卷结构、commit镜像
1.是什么 docker images 镜像是一种轻量级.可执行的独立软件包,用来打包软件运行环境和基于运行环境开发的软件,它包含运行某个软件所需的所有内容,包括代码.运行时.库.环境变量和配置文件. ...
- 移动端H5开发问题记录
1. 当弹出键盘时,会改变页面高度,影响页面样式 通过window.onsize事件可以控制键盘弹出或消失的时候的样式 var h = document.body.scrollHeight // 用o ...
- JDBC (Java DataBase Connectivity)数据库连接池原理解析与实现
一.应用程序直接获取数据库连接的缺点 用户每次请求都需要向数据库获得链接,而数据库创建连接通常需要消耗相对较大的资源,创建时间也较长.假设网站一天10万访问量,数据库服务器就需要创建10万次连接,极大 ...