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:
bottom
will be a string with length in range[2, 8]
.allowed
will 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的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...
随机推荐
- select —— poll —— epoll
import socket,select s=socket.socket() s.setblocking(False) s.setsockopt(socket.SOL_SOCKET,socket. ...
- Linux基础知识之文件的权限(一)
Linux基础知识之文件权限(一) Linux优点之一就是它拥有多用户多任务的环境,在提供文件共享的同时也能保证用户文件的安全性.所以,设置文件的权限管理变得尤为重要. 权限讲解 [der@Der ~ ...
- windows BAT脚本2个服务器间传递文件
1. 脚本功能: 实现2个服务器间文件的传递,例如从A服务器往B服务器上传文件 2. 实现步骤: 2.1 服务器连结,找到指定路径,读取所需要上传的文件,将文件名称复制到一个文件下 (此处考虑可能需要 ...
- [Abp vNext微服务实践] - 启动流程
前几篇分别介绍了abp vNext微服务框架和微服务CI/CD环境搭建,本篇开始介绍vNext微服务框架的开发环境搭建. 环境准备 官方介绍的系统架构图如下: 上图中身份服务和网关服务已经集成在系统中 ...
- vsftpd的安装和配置
1 安装vsftpd sudo apt-get install vsftpd 2 测试是否安装成功 sudo service vsftpd restart 如果有反应即成功 3 彻底卸载vsft ...
- [转]makefile学习
原文: http://blog.fatedier.com/2014/09/08/learn-to-write-makefile-01/ -------------------------------- ...
- django缓存--缓存加数据库型
4.缓存+数据库Session 数据库用于做持久化,缓存用于提高效率 a. 配置 settings.py SESSION_ENGINE = 'django.contrib.sessio ...
- kubeadm 部署kubernetes1.11.1,dashboard1.10.0
---恢复内容开始--- 实验环境准备2台虚拟机: master节点:172.17.1.36 node节点:172.17.1.40 首先安装master节点: master 的虚拟机是全新的机器,在安 ...
- use potato
- 计算(calc.cpp) 这题我搞了2晚上qwq
终于会了!可喜可贺!可喜可贺! 计算(calc.cpp) [问题描述] 小明在你的帮助下,破密了Ferrari设的密码门,正要往前走,突然又出现了一个密码门,门上有一个算式,其中只有“(”,“)” ...