【leetcode】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 triplesallowed. 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'}.
解题思路:我的方法是采用DFS,对于判断是否的题目,用DFS的效率要高于BFS。题目中有一点好像没有说明,就是allowed里面的同一个元素可以用多次。遍历allowed,先找出前两个字符与bottom前两个字符相同的元素;然后bottom去掉第一位,再去allowed中找出前两个字符与bottom前两个字符相同的元素,循环直至bottom长度变成1为止;接下来令bottom等于第一轮中找到的所有的元素的最后一个字符拼接组成的新的字符串,重复第一轮的操作。同时每找到一个可以组成bottom的元素,用一个变量used记录个数,直到userd = len(bottom) * (len(bottom) -1 )/2 表示可以组成如题目要求的bottom。为什么?因为要组成三角形所需的元素个数正好等于 (1+2+3....+最底部bottom的长度)。
代码如下:
class Solution(object):
def pyramidTransition(self, bottom, allowed):
"""
:type bottom: str
:type allowed: List[str]
:rtype: bool
"""
queue = []
dic = {}
for i in allowed:
if bottom[:2] == i[:2]:
queue.append((bottom[1:],i[-1],1))
if i[:2] not in dic:
dic[i[:2]] = [i]
else:
dic[i[:2]].append(i) while len(queue) > 0:
#print len(queue)
bot,path,used = queue.pop(0)
#print used
if used == len(bottom) * (len(bottom) -1 )/2:
return True
elif len(bot) < 2:
queue.insert(0,(path,'',used+1))
else:
if bot[:2] in dic:
for i in dic[bot[:2]]:
queue.insert(0,(bot[1:], path + i[-1], used + 1))
return False
【leetcode】756. Pyramid Transition Matrix的更多相关文章
- 【LeetCode】756. Pyramid Transition Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 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 ...
- 【LeetCode】566. Reshape the Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 变长数组 求余法 维护行列 相似题目 参考资料 日期 ...
- 【LeetCode】519. Random Flip Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/random-fl ...
- 【leetcode】Search a 2D Matrix
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- 【leetcode】 Search a 2D Matrix (easy)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- 【leetcode】566. Reshape the Matrix
原题 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ne ...
- 【leetcode】519. Random Flip Matrix
题目如下: You are given the number of rows n_rows and number of columns n_cols of a 2D binary matrix whe ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
随机推荐
- Django学习——collectstatic错误
Error fetching command 'collectstatic': You're using the staticfiles app without having set the STAT ...
- python3 内置函数enumerate
一.简介: 该函数在字面上是枚举.列举的意思,用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列, 同时列出数据和数据下标,一般用在 for 循环当中,可同时得到数据对象的值及对应的 ...
- Angular JS - 9 - SeaJS加载js模块
seajs加载模块的三种方式 1.seajs.use() 加载入口模块,类似于Java的main函数 2.require: 当在一个模块中需要用到其它模块时一般用require加载 1) ...
- Servlet 第一天
package com.servlet; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet. ...
- 用闭包解决 js 循环中函数变量暂存问题
需求:有一个数组,根据数组的值渲染对应的数字div,单击对应的div 在控制台打印对应的数字.如点击1,控制台打印1. 问题: 不管点击哪个值 打出来都是4 代码如下 <!DOCTYPE htm ...
- Centos7.4 配置之MySQL 8.0【转】
首先查看Mysql最新版本, 此时,目前最新版本为8.0. 开始安装前需要一些准备工作. 1,将本地的MariaDB或者已经安装的MySQL其他版本卸载. (一)卸载本地的本地的MariaDB: 由于 ...
- [BZOJ1572] WorkScheduling
中文题目:工作安排 原文题目:Work Scheduling 传送门 本题可以采用贪心 算法一:按工作时间排序,如果工作能按时完成的工作就按时完成,如果工作不能按时完成就把之前价值最小的工作和当前作比 ...
- 数据挖掘之DecisionTreeClassifier决策树
用决策树DecisionTreeClassifier的数据挖掘算法来通过三个参数,Pclass,Sex,Age,三个参数来求取乘客的获救率. 分为三大步: 一,创建决策树DecisionTreeCla ...
- 单例模式@Singleton在测试中的运用
前言 单例模式是一种比较常用的设计模式,目的是:保证一个类仅有一个实例,并提供一个访问它的全局访问点. 测试种可能用到的场景 : 在很多时候,有些对象我们希望在整个程序只有一个实例,如线程池.数据库连 ...
- 测开之路三十三:Flask实现扎金花游戏
访问http://localhost:8888/game随机获取一张扑克牌.豹子(炸弹):三张点相同的牌.例:AAA.222.顺金(同花顺.色托):花色相同的顺子.例:黑桃456.红桃789.最大的顺 ...