首页 > 其他分享 >LeetCode 756. Pyramid Transition Matrix

LeetCode 756. Pyramid Transition Matrix

时间:2024-07-30 13:39:32浏览次数:14  
标签:Pyramid return Matrix bottom Transition length allowed block string

原题链接在这里: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 }

 

标签:Pyramid,return,Matrix,bottom,Transition,length,allowed,block,string
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18332177

相关文章

  • 仅将 sympy Matrix 的上三角值从 numpy.triu() 复制到数组中?
    我有一个方阵A(可以是任何大小),我想获取上三角部分并将这些值放入一个数组中,而没有中心对角线下方的值(k=0)。A=sympy.Matrix([[4,0,3],[2,4,-2],[-2,-3,7]])使用A_upper=numpy.triu(A)让我A_Upper=sympy.M......
  • 0054_Spiral-Matrix【M】
    JY:矩阵螺旋式遍历(一圈圈螺旋式、从外到里)参考:0054_Spiral-Matrix【M】·语雀1、基于矩阵4个边界指针实现顺时针顺序一层层遍历,共需遍历math.ceil(min(m,n)/2)圈fromtypingimportList,DictclassSolution:defspiralOrder(self,matrix:List[Lis......
  • 0059_Spiral-Matrix-ii【M】
    JY:矩阵的螺旋遍历相似题:0054_Spiral-Matrix【M】参考:0059_Spiral-Matrix-ii【M】 1、基于4个边界指针参考0054_Spiral-Matrix【M】中的解法2classSolution:defgenerateMatrix(self,n:int)->[[int]]:left,right,top,bottom=0,n-1,0,n......
  • Vova Escapes the Matrix
    做的时候就差如何得出一个点到两个不同的出口的最短路和次短路了啊分类讨论如果图不能到达出口,那么可以把所有'.'都填了如果图只能达到一个出口,那么就是所有'.'的个数减去起点到这个出口的最短路如果图可以到达两个及以上出口,考虑填满陷阱之后,图长成什么样子:此时一定刚好还剩......
  • JavaScript Program to print pyramid pattern (打印金字塔图案的程序)
     编写程序打印由星星组成的金字塔图案 例子: 输入:n=6输出:    *    **    ***    ****    *****    ******     *****    ****    ***    **    ......
  • PHP Program to print pyramid pattern (打印金字塔图案的程序)
     编写程序打印由星星组成的金字塔图案 例子: 输入:n=6输出:    *    **    ***    ****    *****    ******     *****    ****    ***    **    ......
  • G. A/B Matrix
    原题链接题解每行有a个,所以总共有\(n\cdota\)个每列有b个,所以总共有\(m\cdotb\)个所以要满足\(na=mb\)想象一下这个场景:每一行,每次往当前列中,最左端的一最少的列的开始连续放置1code#include<bits/stdc++.h>#definelllonglongusingnamespacestd;voids......
  • [LeetCode] 1380. Lucky Numbers in a Matrix
    Givenanmxnmatrixofdistinctnumbers,returnallluckynumbersinthematrixinanyorder.Aluckynumberisanelementofthematrixsuchthatitistheminimumelementinitsrowandmaximuminitscolumn.Example1:Input:matrix=[[3,7,8],[9,11,......
  • manim边学边做--Matrix
    在代数问题中,矩阵是必不可少的工具,manim中提供了一套展示矩阵(Matrix)的模块,专门用于在动画中显示矩阵格式的数据。关于矩阵的类主要有4个:Matrix:通用的矩阵IntegerMatrix:元素是整数的矩阵DecimalMatrix:元素包含小数的矩阵MobjectMatrix:元素可以是图形的矩阵其实IntegerMatrix......
  • c++ Program to print pyramid pattern (打印金字塔图案的程序)
    编写程序打印由星星组成的金字塔图案 例子: 输入:n=6输出:    *    **    ***    ****    *****    ******     *****    ****    ***    **     *......