{"type": "session", "harness": "opencode", "id": "cp-10059", "name": "LeetCode 0554: Brick Wall"} {"type": "message", "message": {"role": "system", "content": "You are an expert LeetCode problem solver. You solve problems efficiently with clean code."}} {"type": "message", "message": {"role": "user", "content": "Problem: Brick Wall (Difficulty: Medium)\nTags: Array, Hash Table\n\nThere 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.\nDraw 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.\nGiven the 2D array wall that contains the information about the wall, return the minimum number of crossed bricks after drawing such a vertical line.\n\nExample 1:\nInput: wall = [[1,2,2,1],[3,1,2],[1,3,2],[2,4],[3,1,2],[1,3,1,1]]\nOutput: 2\n\nExample 2:\nInput: wall = [[1],[1],[1]]\nOutput: 3\n\nConstraints:\n- n == wall.length\n- 1 <= n <= 10^4\n- 1 <= wall[i].length <= 10^4\n- 1 <= sum(wall[i].length) <= 2 * 10^4\n- sum(wall[i]) is the same for each row i.\n- 1 <= wall[i][j] <= 2^31 - 1"}} {"type": "message", "message": {"role": "assistant", "content": "# Time: O(n), n is the total number of the bricks\n# Space: O(m), m is the total number different widths\n\nimport collections\n\n\nclass Solution(object):\n def leastBricks(self, wall):\n \"\"\"\n :type wall: List[List[int]]\n :rtype: int\n \"\"\"\n widths = collections.defaultdict(int)\n result = len(wall)\n for row in wall:\n width = 0\n for i in range(len(row)-1):\n width += row[i]\n widths[width] += 1\n result = min(result, len(wall) - widths[width])\n return result\n\n"}}