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 }

 

posted @ 2024-07-30 13:35  Dylan_Java_NYC  阅读(12)  评论(0编辑  收藏  举报