756. Pyramid Transition Matrix Medium
1/**
2 * [756] Pyramid Transition Matrix
3 *
4 * 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.
5 * 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.
6 *
7 * 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.
8 *
9 * You start with a bottom row of blocks bottom, given as a single string, that you must use as the base of the pyramid.
10 * 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.
11 *
12 * Example 1:
13 * <img alt="" src="https://assets.leetcode.com/uploads/2021/08/26/pyramid1-grid.jpg" style="width: 600px; height: 232px;" />
14 * Input: bottom = "BCD", allowed = ["BCC","CDE","CEA","FFF"]
15 * Output: true
16 * Explanation: The allowed triangular patterns are shown on the right.
17 * Starting from the bottom (level 3), we can build "CE" on level 2 and then build "A" on level 1.
18 * There are three triangular patterns in the pyramid, which are "BCC", "CDE", and "CEA". All are allowed.
19 *
20 * Example 2:
21 * <img alt="" src="https://assets.leetcode.com/uploads/2021/08/26/pyramid2-grid.jpg" style="width: 600px; height: 359px;" />
22 * Input: bottom = "AAAA", allowed = ["AAB","AAC","BCD","BBE","DEF"]
23 * Output: false
24 * Explanation: The allowed triangular patterns are shown on the right.
25 * 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.
26 *
27 *
28 * Constraints:
29 *
30 * 2 <= bottom.length <= 6
31 * 0 <= allowed.length <= 216
32 * allowed[i].length == 3
33 * The letters in all input strings are from the set {'A', 'B', 'C', 'D', 'E', 'F'}.
34 * All the values of allowed are unique.
35 *
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/pyramid-transition-matrix/
40// discuss: https://leetcode.com/problems/pyramid-transition-matrix/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45 pub fn pyramid_transition(bottom: String, allowed: Vec<String>) -> bool {
46 false
47 }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55
56 #[test]
57 fn test_756() {
58 }
59}
60
Back
© 2025 bowen.ge All Rights Reserved.