You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0500-0599/0554.Brick Wall/README_EN.md
+80-36
Original file line number
Diff line number
Diff line change
@@ -56,7 +56,15 @@ tags:
56
56
57
57
<!-- solution:start -->
58
58
59
-
### Solution 1
59
+
### Solution 1: Hash Table + Prefix Sum
60
+
61
+
We can use a hash table $\textit{cnt}$ to record the prefix sum of each row except for the last brick. The key is the value of the prefix sum, and the value is the number of times the prefix sum appears.
62
+
63
+
Traverse each row, and for each brick in the current row, add it to the current prefix sum and update $\textit{cnt}$.
64
+
65
+
Finally, we traverse $\textit{cnt}$ to find the prefix sum that appears the most times, which represents the situation where the least number of bricks are crossed. The final answer is the number of rows in the brick wall minus the number of bricks crossed.
66
+
67
+
The time complexity is $O(m \times n)$, and the space complexity is $O(n)$. Here, $m$ and $n$ are the number of rows and the number of bricks in the brick wall, respectively.
0 commit comments