There is a rectangular brick wall in front of you with n rows of bricks. The ith row has some number of bricks each of the same height (i.e., one unit) but they can be of different widths. The total width of each row is the same.
Draw a vertical line from the top to the bottom and cross the least bricks. If your line goes through the edge of a brick, then the brick is not considered as crossed. You cannot draw a line just along one of the two vertical edges of the wall, in which case the line will obviously cross no bricks.
Given the 2D array wall that contains the information about the wall, return the minimum number of crossed bricks after drawing such a vertical line.
The main idea is to find a vertical line that passes through the maximum number of brick edges (gaps) between bricks, so it crosses the fewest bricks. For each row, we compute the prefix sum of brick widths to find the positions of the gaps (excluding the last edge, which is the wall’s end). We use a hash map to count how many times each gap position occurs. The optimal line is drawn at the gap position with the highest count, so the minimum number of crossed bricks is number of rows - max gap count.