Pyramid Transition Matrix - Problem
Imagine you're an ancient architect tasked with building a sacred pyramid using colored stone blocks. Each block has a unique color represented by a single letter (A, B, C, etc.).
The pyramid follows strict architectural rules:
- Each level has one fewer block than the level below it
- Blocks are perfectly centered on the level beneath
- Most importantly, only specific triangular patterns are allowed by the ancient building codes
You're given:
bottom: A string representing the bottom row of blocks (e.g., "AABA")allowed: A list of 3-character strings representing valid patterns (e.g., ["ABC", "ABD", "BAC"])
Each pattern like "ABC" means: if you have block 'A' on the left and block 'B' on the right at the bottom, you can place block 'C' on top of them.
Goal: Determine if you can build the complete pyramid from bottom to top (ending with a single block) such that every triangular pattern used is in the allowed list.
Return true if the pyramid can be completed, false otherwise.
Input & Output
example_1.py โ Simple Valid Pyramid
$
Input:
bottom = "AABA", allowed = ["ABC", "ACA", "CAA", "AAB"]
โบ
Output:
true
๐ก Note:
We can build: AABA โ ACA โ AA โ A. First level: A+AโA (AAB), A+BโC (ABC), B+AโA (not needed). Second level: A+CโA (ACA). Third level: A+AโA (AAB). Success!
example_2.py โ No Valid Path
$
Input:
bottom = "AABA", allowed = ["ABC", "BAC"]
โบ
Output:
false
๐ก Note:
Starting with AABA, we need patterns for AA, AB, and BA pairs. We have ABC (A+BโC) and BAC (B+AโC), but no pattern for AA. Cannot build the first level up from bottom.
example_3.py โ Single Block Base
$
Input:
bottom = "A", allowed = ["ABC", "DEF"]
โบ
Output:
true
๐ก Note:
Edge case: If bottom already has only one block, the pyramid is already complete. No building required, so return true regardless of allowed patterns.
Constraints
- 2 โค bottom.length โค 6
- 0 โค allowed.length โค 216
- allowed[i].length == 3
- The letters in all input strings are from the set {'A', 'B', 'C', 'D', 'E', 'F'}
- All values of allowed are unique
Visualization
Tap to expand
Understanding the Visualization
1
Parse Patterns
Convert allowed patterns into a lookup map: (left, right) โ [possible tops]
2
Start from Bottom
Begin with the given bottom row and identify all adjacent pairs
3
Build Next Level
For each adjacent pair, look up valid top blocks from our pattern map
4
Try Combinations
Use backtracking to try different combinations of valid blocks
5
Recurse or Backtrack
If a complete level is built, recurse to next level; otherwise backtrack
6
Memoization
Cache results for each row configuration to avoid redundant calculations
Key Takeaway
๐ฏ Key Insight: By using memoized backtracking with a pattern lookup table, we can efficiently explore all possible pyramid constructions while avoiding redundant calculations, achieving optimal performance for this constraint-satisfaction problem.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code