Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 24 additions & 14 deletions leetcode/701-800/0756.Pyramid-Transition-Matrix/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
# [756.Pyramid Transition Matrix][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## 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.

**Example 1:**
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:**

![1](./1.jpg)

```
Input: a = "11", b = "1"
Output: "100"
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:**

## 题解
![2](./2.jpg)

### 思路1
> ...
Pyramid Transition Matrix
```go
```

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.
```

## 结语

Expand Down
42 changes: 40 additions & 2 deletions leetcode/701-800/0756.Pyramid-Transition-Matrix/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(bottom string, allowed []string) bool {
allowedMap := make(map[[2]byte][]byte)
for _, pattern := range allowed {
key := [2]byte{pattern[0], pattern[1]}
allowedMap[key] = append(allowedMap[key], pattern[2])
}

var canBuild func(string) bool
canBuild = func(current string) bool {
if len(current) == 1 {
return true
}

var nextLayers []string

var backtrack func(int, []byte)
backtrack = func(index int, path []byte) {
if index == len(current)-1 {
nextLayers = append(nextLayers, string(path))
return
}
pair := [2]byte{current[index], current[index+1]}
if tops, exists := allowedMap[pair]; exists {
for _, top := range tops {
backtrack(index+1, append(path, top))
}
}
}

backtrack(0, []byte{})

for _, nextLayer := range nextLayers {
if canBuild(nextLayer) {
return true
}
}
return false
}

return canBuild(bottom)
}
22 changes: 11 additions & 11 deletions leetcode/701-800/0756.Pyramid-Transition-Matrix/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
bottom string
allowed []string
expect bool
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "BCD", []string{"BCC", "CDE", "CEA", "FFF"}, true},
{"TestCase2", "AAAA", []string{"AAB", "AAC", "BCD", "BBE", "DEF"}, false},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.bottom, c.allowed)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.bottom, c.allowed)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading