|
11 | 11 |
|
12 | 12 | ## 756. Pyramid Transition Matrix (Medium)
|
13 | 13 |
|
14 | | -<p> |
15 | | -We are stacking blocks to form a pyramid. Each block has a color which is a one letter string, like `'Z'`. |
16 | | -</p><p> |
17 | | -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. |
18 | | -</p><p> |
19 | | -We start with a bottom row of <code>bottom</code>, represented as a single string. We also start with a list of allowed triples <code>allowed</code>. Each allowed triple is represented as a string of length 3. |
20 | | -</p><p> |
21 | | -Return true if we can build the pyramid all the way to the top, otherwise false. |
22 | | -</p> |
23 | | - |
24 | | -<p><b>Example 1:</b><br /> |
| 14 | +<p>We are stacking blocks to form a pyramid. Each block has a color which is a one letter string.</p> |
| 15 | + |
| 16 | +<p>We are allowed to place any color block <code>C</code> on top of two adjacent blocks of colors <code>A</code> and <code>B</code>, if and only if <code>ABC</code> is an allowed triple.</p> |
| 17 | + |
| 18 | +<p>We start with a bottom row of <code>bottom</code>, represented as a single string. We also start with a list of allowed triples <code>allowed</code>. Each allowed triple is represented as a string of length 3.</p> |
| 19 | + |
| 20 | +<p>Return true if we can build the pyramid all the way to the top, otherwise false.</p> |
| 21 | + |
| 22 | +<p><b>Example 1:</b></p> |
| 23 | + |
25 | 24 | <pre>
|
26 | | -<b>Input:</b> bottom = "XYZ", allowed = ["XYD", "YZE", "DEA", "FFF"] |
| 25 | +<b>Input:</b> bottom = "BCD", allowed = ["BCG", "CDE", "GEA", "FFF"] |
27 | 26 | <b>Output:</b> true
|
28 | 27 | <b>Explanation:</b>
|
29 | 28 | We can stack the pyramid like this:
|
30 | 29 | A
|
31 | 30 | / \
|
32 | | - D E |
| 31 | + G E |
33 | 32 | / \ / \
|
34 | | -X Y Z |
| 33 | +B C D |
35 | 34 |
|
36 | | -This works because ('X', 'Y', 'D'), ('Y', 'Z', 'E'), and ('D', 'E', 'A') are allowed triples. |
37 | | -</pre> |
38 | | -</p> |
| 35 | +We are allowed to place G on top of B and C because BCG is an allowed triple. Similarly, we can place E on top of C and D, then A on top of G and E.</pre> |
| 36 | + |
| 37 | +<p> </p> |
| 38 | + |
| 39 | +<p><b>Example 2:</b></p> |
39 | 40 |
|
40 | | -<p><b>Example 2:</b><br /> |
41 | 41 | <pre>
|
42 | | -<b>Input:</b> bottom = "XXYX", allowed = ["XXX", "XXY", "XYX", "XYY", "YXZ"] |
| 42 | +<b>Input:</b> bottom = "AABA", allowed = ["AAA", "AAB", "ABA", "ABB", "BAC"] |
43 | 43 | <b>Output:</b> false
|
44 | 44 | <b>Explanation:</b>
|
45 | | -We can't stack the pyramid to the top. |
| 45 | +We can't stack the pyramid to the top. |
46 | 46 | Note that there could be allowed triples (A, B, C) and (A, B, D) with C != D.
|
47 | 47 | </pre>
|
48 | | -</p> |
49 | 48 |
|
50 | | -<p><b>Note:</b><br> |
| 49 | +<p> </p> |
| 50 | + |
| 51 | +<p><b>Note:</b></p> |
| 52 | + |
51 | 53 | <ol>
|
52 | | -<li><code>bottom</code> will be a string with length in range <code>[2, 8]</code>.</li> |
53 | | -<li><code>allowed</code> will have length in range <code>[0, 200]</code>.</li> |
54 | | -<li>Letters in all strings will be chosen from the set <code>{'A', 'B', 'C', 'D', 'E', 'F', 'G'}</code>.</li> |
| 54 | +<li><code>bottom</code> will be a string with length in range <code>[2, 8]</code>.</li> |
| 55 | +<li><code>allowed</code> will have length in range <code>[0, 200]</code>.</li> |
| 56 | +<li>Letters in all strings will be chosen from the set <code>{'A', 'B', 'C', 'D', 'E', 'F', 'G'}</code>.</li> |
55 | 57 | </ol>
|
56 | | -</p> |
| 58 | + |
| 59 | +<p> </p> |
57 | 60 |
|
58 | 61 | ### Related Topics
|
59 | 62 | [[Bit Manipulation](https://github.com/openset/leetcode/tree/master/tag/bit-manipulation/README.md)]
|
|
0 commit comments