Skip to content

Commit e52ebff

Browse files
committed
Add solution and test-cases for problem 756
1 parent 7ce34f6 commit e52ebff

File tree

5 files changed

+75
-27
lines changed

5 files changed

+75
-27
lines changed
22.1 KB
Loading
29.6 KB
Loading

leetcode/701-800/0756.Pyramid-Transition-Matrix/README.md

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
# [756.Pyramid Transition Matrix][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
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+
6+
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.
7+
8+
- 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.
9+
10+
You start with a bottom row of blocks `bottom`, given as a single string, that you **must** use as the base of the pyramid.
711

8-
**Example 1:**
12+
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.
13+
14+
**Example 1:**
15+
16+
![1](./1.jpg)
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
19+
Input: bottom = "BCD", allowed = ["BCC","CDE","CEA","FFF"]
20+
Output: true
21+
Explanation: The allowed triangular patterns are shown on the right.
22+
Starting from the bottom (level 3), we can build "CE" on level 2 and then build "A" on level 1.
23+
There are three triangular patterns in the pyramid, which are "BCC", "CDE", and "CEA". All are allowed.
1324
```
1425

15-
## 题意
16-
> ...
26+
**Example 2:**
1727

18-
## 题解
28+
![2](./2.jpg)
1929

20-
### 思路1
21-
> ...
22-
Pyramid Transition Matrix
23-
```go
2430
```
25-
31+
Input: bottom = "AAAA", allowed = ["AAB","AAC","BCD","BBE","DEF"]
32+
Output: false
33+
Explanation: The allowed triangular patterns are shown on the right.
34+
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.
35+
```
2636

2737
## 结语
2838

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,43 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(bottom string, allowed []string) bool {
4+
allowedMap := make(map[[2]byte][]byte)
5+
for _, pattern := range allowed {
6+
key := [2]byte{pattern[0], pattern[1]}
7+
allowedMap[key] = append(allowedMap[key], pattern[2])
8+
}
9+
10+
var canBuild func(string) bool
11+
canBuild = func(current string) bool {
12+
if len(current) == 1 {
13+
return true
14+
}
15+
16+
var nextLayers []string
17+
18+
var backtrack func(int, []byte)
19+
backtrack = func(index int, path []byte) {
20+
if index == len(current)-1 {
21+
nextLayers = append(nextLayers, string(path))
22+
return
23+
}
24+
pair := [2]byte{current[index], current[index+1]}
25+
if tops, exists := allowedMap[pair]; exists {
26+
for _, top := range tops {
27+
backtrack(index+1, append(path, top))
28+
}
29+
}
30+
}
31+
32+
backtrack(0, []byte{})
33+
34+
for _, nextLayer := range nextLayers {
35+
if canBuild(nextLayer) {
36+
return true
37+
}
38+
}
39+
return false
40+
}
41+
42+
return canBuild(bottom)
543
}

leetcode/701-800/0756.Pyramid-Transition-Matrix/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
bottom string
14+
allowed []string
15+
expect bool
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", "BCD", []string{"BCC", "CDE", "CEA", "FFF"}, true},
18+
{"TestCase2", "AAAA", []string{"AAB", "AAC", "BCD", "BBE", "DEF"}, false},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.bottom, c.allowed)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.bottom, c.allowed)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)