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的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...
随机推荐
- C# 修改Config文件,增删查改
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/BYH371256/article/det ...
- vue 之this.$router.push、replace、go的区别
一.this.$router.push 说明:跳转到指定URL,向history栈添加一个新的记录,点击后退会返回至上一个页面 使用: this.$router.push('/index') this ...
- windows配置mysql权限
想要把csv里面的数据导入mysql,报错.用命令 show variables like '%secure%'; : [2019-04-08 17:04:36] [HY000][129 ...
- vue-重要方法使用
标签属性router-link-exact-active: 当页面正在方位是触发router-link-exact-active 标签router-link to= 使用a标签页面就会跳转,就不是单页 ...
- 简单的理解 StringBuffer/StringBuilder/String 的区别
StringBuffer/StringBuilder/String 的区别 这个三类之间主要的区别:运行速度,线程安全两个方面. 速度方面(快到慢): StringBuilder > Strin ...
- Codeforces Round #589 (Div. 2) C - Primes and Multiplication(数学, 质数)
链接: https://codeforces.com/contest/1228/problem/C 题意: Let's introduce some definitions that will be ...
- 题解 [CF891C] Envy
题面 解析 首先根据Kruskal算法, 我们可以知道, 在加入权值为\(w\)的边时, 权值小于\(w\)的边都已经加进树里了(除了连成环的). 所以,我们可以保存一下每条边的端点在加入生成树之前的 ...
- 部署lnmp
装包 1.安装依赖包 yum - y install gcc openssl-devel pcre-devel zlib-devel 2.解源码包 .tar.gz 3.切换到解压缩后的目录,配置参数 ...
- 关于.ssh出错,无法从远程git仓库拉代码
背景:从windows开发环境改到ubuntu,将windows下生成的.ssh文件拷贝到ubuntu下,以clone远程git仓库代码 错误:sign_and_send_pubkey: signin ...
- msyql round函数隐藏问题
1.背景 在用mysql round进行四舍五入计算的时候如果参与计算的字段为float,则最终计算出的四舍五入效果会有很大出入.例子我就不列举了 2.原因 mysql官方文档中关于ROUND函数的部 ...