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
Pyramid Construction ProcessAABAACAAAAPattern MappingAA โ†’ A (from AAB)AB โ†’ C (from ABC)BA โ†’ A (from ???)AC โ†’ A (from ACA)CA โ†’ A (from CAA)โœ“ Success!All levels built successfullyPyramid construction completeBottom RowLevel 2Level 3Top
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.
Asked in
Google 15 Meta 8 Amazon 6 Microsoft 4
28.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen