Leetcode Problem 2184. Number of Ways to Build Sturdy Brick Wall

2184. Number of Ways to Build Sturdy Brick Wall

Leetcode Solutions

DFS + Top-Down DP

Algorithm

  1. Generate all possible rows (bitmasks) that can be built with the given bricks and width.
  2. Create a list of valid transitions between rows, where a transition is valid if the bitmasks do not overlap (except at the ends).
  3. Use a top-down DP approach to count the number of ways to build the wall from the bottom up.
  4. For each row, recursively count the number of ways to build the wall above it using the valid transitions.
  5. Cache the results of the DP to avoid recomputation.
  6. Return the total count of ways to build the wall of the given height.
UML Thumbnail

DFS with Adjacency List

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...