Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2184: Number of Ways to Build Sturdy Brick Wall

In this guide, we solve Leetcode #2184 Number of Ways to Build Sturdy Brick Wall in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

You are given integers height and width which specify the dimensions of a brick wall you are building. You are also given a 0-indexed array of unique integers bricks, where the ith brick has a height of 1 and a width of bricks[i].

Quick Facts

  • Difficulty: Medium
  • Premium: Yes
  • Tags: Bit Manipulation, Array, Dynamic Programming, Bitmask

Intuition

The problem breaks into overlapping subproblems, so caching results prevents exponential repetition.

A carefully chosen DP state captures exactly what we need to build the final answer.

Approach

Define the DP state and recurrence, then compute states in the correct order.

Optionally compress space once the recurrence is clear.

Steps:

  • Choose a DP state definition.
  • Write the recurrence and base cases.
  • Compute states in the correct order.

Example

Input: height = 2, width = 3, bricks = [1,2] Output: 2 Explanation: The first two walls in the diagram show the only two ways to build a sturdy brick wall. Note that the third wall in the diagram is not sturdy because adjacent rows join bricks 2 units from the left.

Python Solution

class Solution: def buildWall(self, height: int, width: int, bricks: List[int]) -> int: def dfs(v): if v > width: return if v == width: s.append(t[:]) return for x in bricks: t.append(x) dfs(v + x) t.pop() def check(a, b): s1, s2 = a[0], b[0] i = j = 1 while i < len(a) and j < len(b): if s1 == s2: return False if s1 < s2: s1 += a[i] i += 1 else: s2 += b[j] j += 1 return True mod = 10**9 + 7 s = [] t = [] dfs(0) g = defaultdict(list) n = len(s) for i in range(n): if check(s[i], s[i]): g[i].append(i) for j in range(i + 1, n): if check(s[i], s[j]): g[i].append(j) g[j].append(i) dp = [[0] * n for _ in range(height)] for j in range(n): dp[0][j] = 1 for i in range(1, height): for j in range(n): for k in g[j]: dp[i][j] += dp[i - 1][k] dp[i][j] %= mod return sum(dp[-1]) % mod

Complexity

The time complexity is O(n·m) (typical). The space complexity is O(n·m) or optimized.

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy