LeetCode 756. Pyramid Transition Matrix

原题链接在这里:https://leetcode.com/problems/pyramid-transition-matrix/description/

题目:

You are stacking blocks to form a pyramid. Each block has a color, which is represented by a single letter. Each row of blocks contains one less block than the row beneath it and is centered on top.

To make the pyramid aesthetically pleasing, there are only specific triangular patterns that are allowed. A triangular pattern consists of a single block stacked on top of two blocks. The patterns are given as a list of three-letter strings allowed, where the first two characters of a pattern represent the left and right bottom blocks respectively, and the third character is the top block.

  • For example, "ABC" represents a triangular pattern with a 'C' block stacked on top of an 'A' (left) and 'B' (right) block. Note that this is different from "BAC" where 'B' is on the left bottom and 'A' is on the right bottom.

You start with a bottom row of blocks bottom, given as a single string, that you must use as the base of the pyramid.

Given bottom and allowed, return true if you can build the pyramid all the way to the top such that every triangular pattern in the pyramid is in allowed, or false otherwise.

Example 1:

Input: bottom = "BCD", allowed = ["BCC","CDE","CEA","FFF"]
Output: true
Explanation: The allowed triangular patterns are shown on the right.
Starting from the bottom (level 3), we can build "CE" on level 2 and then build "A" on level 1.
There are three triangular patterns in the pyramid, which are "BCC", "CDE", and "CEA". All are allowed.

Example 2:

Input: bottom = "AAAA", allowed = ["AAB","AAC","BCD","BBE","DEF"]
Output: false
Explanation: The allowed triangular patterns are shown on the right.
Starting from the bottom (level 4), there are multiple ways to build level 3, but trying all the possibilites, you will get always stuck before building level 1.

Constraints:

  • 2 <= bottom.length <= 6
  • 0 <= allowed.length <= 216
  • allowed[i].length == 3
  • The letters in all input strings are from the set {'A', 'B', 'C', 'D', 'E', 'F'}.
  • All the values of allowed are unique.

题解:

The question requests to check if the blocks can be converted to one block on the top.

We need to first convert the allowed list into a map.

And continue checking with DFS. 

DFS needs to return boolean indicating if it can be convertd to one block on the top.

DFS state needs bottom string, current index, next string, and converted map.

If the bottom string length == 1, we have a solid answer, return true.

Otherwise, check if next string length == current bottom string length - 1, if yes, continues with next string.

Otherwise, with current index, we get the key string, for each corresponding char in the map, continue DFS. 

Time Complexity: exponential. 

Space: O(m + n). m = allowed.size(). n = bottom.length(), we need n level stack. 

AC Java:

 1 class Solution {
 2     public boolean pyramidTransition(String bottom, List<String> allowed) {
 3         Map<String, Set<Character>> hm = new HashMap<>();
 4         for(String s : allowed){
 5             hm.computeIfAbsent(s.substring(0, 2), k -> new HashSet<Character>()).add(s.charAt(2));
 6         }
 7 
 8         return dfs(bottom, 0, "", hm);
 9     }
10 
11     private boolean dfs(String bottom, int ind, String next, Map<String, Set<Character>> hm){
12         if(bottom.length() == 1){
13             return true;
14         }
15 
16         if(next.length() == bottom.length() - 1){
17             return dfs(next, 0, "", hm);
18         }
19 
20         String key = bottom.substring(ind, ind + 2);
21         for(char c : hm.getOrDefault(key, new HashSet<>())){
22             if(dfs(bottom, ind + 1, next + c, hm)){
23                 return true;
24             }
25         }
26 
27         return false;
28     }
29 }

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/774032.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

两个燃点,引爆在线教育平台数智化

人人都能消费数据,事事都能数据驱动,正在成为火花思维数智化升级下的日常,这也与火山引擎推出的企业数智化升级新模式不谋而合。关注 字节跳动数据平台公众号,回复“火花思维”,获取高清方案设计及产品资料 2017年,专注于少儿逻辑思维的在线教育品牌火花思维正式成立,依…

一个简单的遍历需求

今天开发一个前端遍历展示的接口,被前端怼了看图需求很简单多行展示 每行多个,可显示更多,每行可左右滑动为了图省事简单组装一下直接返回回去了,方便是真的方便,前端生气也是真的生气,可以简单对这个横向、纵向接口压缩一下横向空间上,可以取分页,前端自己传,无论是懒…

分享一个MySQL数据库表结构导出word文档最方便的方法

原文链接:https://blog.csdn.net/typ1805/article/details/83658708 1、使用的是MySQL-Front工具,这个工具使用非常方便,尤其是导出数据的时候,几百万的数据一两分钟就导完了,推荐使用。 MySQL-Front下载(只有3.93M):https://mysql-front.en.softonic.com/ 注:新版本和…

排球比赛计分程序模拟冲刺(sprint)

模拟冲刺计划(spring) 选择小的用户故事模拟冲刺: 裁判张三、业余排球比赛组织者李四、排球运动员王五 一、任务拆分与开发时间:裁判张三:任务 1:开发操作指南和演示视频- 时间:第 1 天 任务 2:实现提前设置比赛基本信息功能- 时间:第 2 天 任务 3:设计简便计分操作界面…

代码随想录day14 || 226 翻转二叉树,101 对称二叉树, 104 二叉树的最大深度, 111 二叉树的最小深度

226 翻转二叉树 func invertTree(root *TreeNode) *TreeNode {// 思考,广度优先遍历,对于每一层,翻转其左右子节点if root == nil {return nil}queue := list.New()queue.PushBack(root)size := 1 // 存储每一层的节点个数for queue.Len() > 0{var count intfor i:=0; i&…

【数值计算方法】数值积分微分

《现代数值计算方法 第二版》1. 引言 高数中计算积分思路基本是牛顿莱布尼兹法: \[I[f]=\int_{a}^{b}f(x)\mathrm{d}x=F(b)-F(a), \]\[F^{\prime}(x)=f(x). \]实际计算中,原函数一般无法求出.给不出解析解,只能求出数值解. 设在区间 [a,b]( 不妨先设 a,b 为有限数 ) 上 ,\(f(x)…

经典的反转

def reverseArr(arr, start, end):while(start < end):arr[start], arr[end] = arr[end], arr[start]start +=1;end -=1def fun(arr,d, n):reverseArr(arr,0, n-1)reverseArr(arr, n-d, n-1)reverseArr(arr,0, n-d-1)arr = [1,2,3,4,5,6,7] fun(arr,2,len(arr)) print(arr)

【日记】今天又是哪朵小云不开心了呀(1886 字)

正文上午上班没多久,天就特别阴,感觉像是要下暴雨的样子。前台接了一个电话,家里人打来的,她妈妈叮嘱她,要注意一点。他们那边已经开始下了。她转过头对我笑笑说,原来下雨在一个城里也能不同步。当时我笑了笑,对她说,局部降雨还有更局部的,然后打开的视频网站,随便点…

51nod两问-Pinball等

问题1-Pinball为什么这样解释的通,我看不懂什么意思?还有这个 \(e\) 在后面状态中没有体现。具体做法?为什么只有 \([a_i,c_i]\) 需要考虑?他可以往左边掉。那么从 \(n\) 开始掉又如何考虑 Kamp手绘的图:这个图似乎就不满足了。不知道什么意思。这个思路怎么做。

2024.7.26模拟赛8

模拟赛 抽象比赛,含金量最高的是题目背景? 好像还是连续的。。。 T1 Same Integers 题目背景签到题,因为只有加操作,目标是将两个较小的数加成最大的。 根据差的奇偶判断能否加二得到,如果不能先加一调一下。(简单题,题解抽象一点也没事吧)code #include<bits/stdc+…

大道至简读后有感

《大道至简》读后有感 作为一名初学计算机语言的学生,在语言的学习过程中,总会有各种问题困扰我。在阅读《大道至简》这本经典软件工程读物之前,我过去常常是追求复杂性和繁琐的代码设计。因为我觉得这样能体现我的个人水平,更“高级”之类的,我会为了实现某个功能而不断添…