bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 554. Brick Wall

554. Brick Wall

Leetcode Solutions

Approach # Using HashMap

  1. Initialize a HashMap edges to store the frequency of each cumulative width (edge) encountered.
  2. Initialize maxEdges to 0 to keep track of the maximum number of edges aligned.
  3. Iterate over each row in the wall: a. Set currentWidth to 0, which will store the cumulative width as we go through the bricks in the current row. b. Iterate over the bricks in the current row, excluding the last brick: i. Add the width of the current brick to currentWidth. ii. Increment the count for currentWidth in the edges HashMap. iii. Update maxEdges to be the maximum of itself and the count for currentWidth.
  4. The minimum number of bricks crossed is the total number of rows minus maxEdges.
UML Thumbnail

Approach # Brute Force

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...