Skip to content

Commit 0f2011c

Browse files
committed
+ problem 756
1 parent a79949e commit 0f2011c

File tree

5 files changed

+165
-0
lines changed

5 files changed

+165
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# 756. Pyramid Transition Matrix
2+
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.
3+
4+
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.
5+
6+
* 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.
7+
8+
You start with a bottom row of blocks `bottom`, given as a single string, that you **must** use as the base of the pyramid.
9+
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+
![](https://assets.leetcode.com/uploads/2021/08/26/pyramid1-grid.jpg)
14+
<pre>
15+
<strong>Input:</strong> bottom = "BCD", allowed = ["BCC","CDE","CEA","FFF"]
16+
<strong>Output:</strong> true
17+
<strong>Explanation:</strong> The allowed triangular patterns are shown on the right.
18+
Starting from the bottom (level 3), we can build "CE" on level 2 and then build "A" on level 1.
19+
There are three triangular patterns in the pyramid, which are "BCC", "CDE", and "CEA". All are allowed.
20+
</pre>
21+
22+
#### Example 2:
23+
![](https://assets.leetcode.com/uploads/2021/08/26/pyramid2-grid.jpg)
24+
<pre>
25+
<strong>Input:</strong> bottom = "AAAA", allowed = ["AAB","AAC","BCD","BBE","DEF"]
26+
<strong>Output:</strong> false
27+
<strong>Explanation:</strong> The allowed triangular patterns are shown on the right.
28+
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.
29+
</pre>
30+
31+
#### Constraints:
32+
* `2 <= bottom.length <= 6`
33+
* `0 <= allowed.length <= 216`
34+
* `allowed[i].length == 3`
35+
* The letters in all input strings are from the set `{'A', 'B', 'C', 'D', 'E', 'F'}`.
36+
* All the values of `allowed` are **unique**.
37+
38+
## Solutions (Python)
39+
40+
### 1. Solution
41+
```Python
42+
class Solution:
43+
def pyramidTransition(self, bottom: str, allowed: List[str]) -> bool:
44+
@cache
45+
def buildFromBottom(bottom: str) -> bool:
46+
if len(bottom) == 1:
47+
return True
48+
49+
top = [''] * (len(bottom) - 1)
50+
51+
def buildTop(i: int) -> bool:
52+
if i >= len(top):
53+
return buildFromBottom(''.join(top))
54+
55+
for pattern in allowed:
56+
if bottom[i:i + 2] == pattern[:2]:
57+
top[i] = pattern[2]
58+
if buildTop(i + 1):
59+
return True
60+
top[i] = ''
61+
62+
return False
63+
64+
return buildTop(0)
65+
66+
return buildFromBottom(bottom)
67+
```
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# 756. 金字塔转换矩阵
2+
你正在把积木堆成金字塔。每个块都有一个颜色,用一个字母表示。每一行的块比它下面的行 **少一个块** ,并且居中。
3+
4+
为了使金字塔美观,只有特定的 **三角形图案** 是允许的。一个三角形的图案由 **两个块** 和叠在上面的 **单个块** 组成。模式是以三个字母字符串的列表形式 `allowed` 给出的,其中模式的前两个字符分别表示左右底部块,第三个字符表示顶部块。
5+
6+
* 例如,`"ABC"` 表示一个三角形图案,其中一个 `“C”` 块堆叠在一个 `'A'` 块(左)和一个 `'B'` 块(右)之上。请注意,这与 `"BAC"` 不同,`"B"` 在左下角,`"A"` 在右下角。
7+
8+
你从作为单个字符串给出的底部的一排积木 `bottom` 开始,**必须** 将其作为金字塔的底部。
9+
10+
在给定 `bottom``allowed` 的情况下,如果你能一直构建到金字塔顶部,使金字塔中的 **每个三角形图案** 都是在 `allowed` 中的,则返回 `true` ,否则返回 `false`
11+
12+
#### 示例 1:
13+
![](https://assets.leetcode.com/uploads/2021/08/26/pyramid1-grid.jpg)
14+
<pre>
15+
<strong>输入:</strong> bottom = "BCD", allowed = ["BCC","CDE","CEA","FFF"]
16+
<strong>输出:</strong> true
17+
<strong>解释:</strong> 允许的三角形图案显示在右边。
18+
从最底层(第 3 层)开始,我们可以在第 2 层构建“CE”,然后在第 1 层构建“E”。
19+
金字塔中有三种三角形图案,分别是 “BCC”、“CDE” 和 “CEA”。都是允许的。
20+
</pre>
21+
22+
#### 示例 2:
23+
![](https://assets.leetcode.com/uploads/2021/08/26/pyramid2-grid.jpg)
24+
<pre>
25+
<strong>输入:</strong> bottom = "AAAA", allowed = ["AAB","AAC","BCD","BBE","DEF"]
26+
<strong>输出:</strong> false
27+
<strong>解释:</strong> 允许的三角形图案显示在右边。
28+
从最底层(即第 4 层)开始,创造第 3 层有多种方法,但如果尝试所有可能性,你便会在创造第 1 层前陷入困境。
29+
</pre>
30+
31+
#### 提示:
32+
* `2 <= bottom.length <= 6`
33+
* `0 <= allowed.length <= 216`
34+
* `allowed[i].length == 3`
35+
* 所有输入字符串中的字母来自集合 `{'A', 'B', 'C', 'D', 'E', 'F'}`
36+
* `allowed` 中所有值都是 **唯一的**
37+
38+
## 题解 (Python)
39+
40+
### 1. 题解
41+
```Python
42+
class Solution:
43+
def pyramidTransition(self, bottom: str, allowed: List[str]) -> bool:
44+
@cache
45+
def buildFromBottom(bottom: str) -> bool:
46+
if len(bottom) == 1:
47+
return True
48+
49+
top = [''] * (len(bottom) - 1)
50+
51+
def buildTop(i: int) -> bool:
52+
if i >= len(top):
53+
return buildFromBottom(''.join(top))
54+
55+
for pattern in allowed:
56+
if bottom[i:i + 2] == pattern[:2]:
57+
top[i] = pattern[2]
58+
if buildTop(i + 1):
59+
return True
60+
top[i] = ''
61+
62+
return False
63+
64+
return buildTop(0)
65+
66+
return buildFromBottom(bottom)
67+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def pyramidTransition(self, bottom: str, allowed: List[str]) -> bool:
3+
@cache
4+
def buildFromBottom(bottom: str) -> bool:
5+
if len(bottom) == 1:
6+
return True
7+
8+
top = [''] * (len(bottom) - 1)
9+
10+
def buildTop(i: int) -> bool:
11+
if i >= len(top):
12+
return buildFromBottom(''.join(top))
13+
14+
for pattern in allowed:
15+
if bottom[i:i + 2] == pattern[:2]:
16+
top[i] = pattern[2]
17+
if buildTop(i + 1):
18+
return True
19+
top[i] = ''
20+
21+
return False
22+
23+
return buildTop(0)
24+
25+
return buildFromBottom(bottom)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@
546546
[752][752l] |[Open the Lock][752] |![rb]&nbsp;&nbsp;![rs]
547547
[753][753l] |[Cracking the Safe][753] |![py]
548548
[754][754l] |[Reach a Number][754] |![rs]
549+
[756][756l] |[Pyramid Transition Matrix][756] |![py]
549550
[757][757l] |[Set Intersection Size At Least Two][757] |![py]
550551
[762][762l] |[Prime Number of Set Bits in Binary Representation][762] |![py]
551552
[763][763l] |[Partition Labels][763] |![rs]
@@ -2475,6 +2476,7 @@
24752476
[752]:Problemset/0752-Open%20the%20Lock/README.md#752-open-the-lock
24762477
[753]:Problemset/0753-Cracking%20the%20Safe/README.md#753-cracking-the-safe
24772478
[754]:Problemset/0754-Reach%20a%20Number/README.md#754-reach-a-number
2479+
[756]:Problemset/0756-Pyramid%20Transition%20Matrix/README.md#756-pyramid-transition-matrix
24782480
[757]:Problemset/0757-Set%20Intersection%20Size%20At%20Least%20Two/README.md#757-set-intersection-size-at-least-two
24792481
[762]:Problemset/0762-Prime%20Number%20of%20Set%20Bits%20in%20Binary%20Representation/README.md#762-prime-number-of-set-bits-in-binary-representation
24802482
[763]:Problemset/0763-Partition%20Labels/README.md#763-partition-labels
@@ -4398,6 +4400,7 @@
43984400
[752l]:https://leetcode.com/problems/open-the-lock/
43994401
[753l]:https://leetcode.com/problems/cracking-the-safe/
44004402
[754l]:https://leetcode.com/problems/reach-a-number/
4403+
[756l]:https://leetcode.com/problems/pyramid-transition-matrix/
44014404
[757l]:https://leetcode.com/problems/set-intersection-size-at-least-two/
44024405
[762l]:https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/
44034406
[763l]:https://leetcode.com/problems/partition-labels/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@
546546
[752][752l] |[打开转盘锁][752] |![rb]&nbsp;&nbsp;![rs]|
547547
[753][753l] |[破解保险箱][753] |![py]
548548
[754][754l] |[到达终点数字][754] |![rs]
549+
[756][756l] |[金字塔转换矩阵][756] |![py]
549550
[757][757l] |[设置交集大小至少为2][757] |![py]
550551
[762][762l] |[二进制表示中质数个计算置位][762] |![py]
551552
[763][763l] |[划分字母区间][763] |![rs]
@@ -2475,6 +2476,7 @@
24752476
[752]:Problemset/0752-Open%20the%20Lock/README_CN.md#752-打开转盘锁
24762477
[753]:Problemset/0753-Cracking%20the%20Safe/README_CN.md#753-破解保险箱
24772478
[754]:Problemset/0754-Reach%20a%20Number/README_CN.md#754-到达终点数字
2479+
[756]:Problemset/0756-Pyramid%20Transition%20Matrix/README_CN.md#756-金字塔转换矩阵
24782480
[757]:Problemset/0757-Set%20Intersection%20Size%20At%20Least%20Two/README_CN.md#757-设置交集大小至少为2
24792481
[762]:Problemset/0762-Prime%20Number%20of%20Set%20Bits%20in%20Binary%20Representation/README_CN.md#762-二进制表示中质数个计算置位
24802482
[763]:Problemset/0763-Partition%20Labels/README_CN.md#763-划分字母区间
@@ -4398,6 +4400,7 @@
43984400
[752l]:https://leetcode.cn/problems/open-the-lock/
43994401
[753l]:https://leetcode.cn/problems/cracking-the-safe/
44004402
[754l]:https://leetcode.cn/problems/reach-a-number/
4403+
[756l]:https://leetcode.cn/problems/pyramid-transition-matrix/
44014404
[757l]:https://leetcode.cn/problems/set-intersection-size-at-least-two/
44024405
[762l]:https://leetcode.cn/problems/prime-number-of-set-bits-in-binary-representation/
44034406
[763l]:https://leetcode.cn/problems/partition-labels/

0 commit comments

Comments
 (0)