diff --git a/solution/0500-0599/0554.Brick Wall/README.md b/solution/0500-0599/0554.Brick Wall/README.md index 9721672166d43..76f8531a69bd9 100644 --- a/solution/0500-0599/0554.Brick Wall/README.md +++ b/solution/0500-0599/0554.Brick Wall/README.md @@ -56,7 +56,15 @@ tags: -### 方法一 +### 方法一:哈希表 + 前缀和 + +我们可以用一个哈希表 $\textit{cnt}$ 记录每一行除了最后一个砖块以外的前缀和,其中键为前缀和的值,值为该前缀和出现的次数。 + +遍历每一行,对于当前行的每一个砖块,我们将其加到当前的前缀和上,然后更新 $\textit{cnt}$。 + +最后我们遍历 $\textit{cnt}$,找出出现次数最多的前缀和,这就是穿过的砖块数量最少的情况。最后答案即为砖墙的行数减去穿过的砖块数量。 + +时间复杂度 $O(m \times n)$,空间复杂度 $O(n)$。其中 $m$ 和 $n$ 分别是砖墙的行数和砖墙的砖块数。 @@ -65,15 +73,13 @@ tags: ```python class Solution: def leastBricks(self, wall: List[List[int]]) -> int: - cnt = defaultdict(int) + cnt = Counter() for row in wall: - width = 0 - for brick in row[:-1]: - width += brick - cnt[width] += 1 - if not cnt: - return len(wall) - return len(wall) - cnt[max(cnt, key=cnt.get)] + s = 0 + for x in row[:-1]: + s += x + cnt[s] += 1 + return len(wall) - max(cnt.values(), default=0) ``` #### Java @@ -82,38 +88,79 @@ class Solution: class Solution { public int leastBricks(List> wall) { Map cnt = new HashMap<>(); - for (List row : wall) { - int width = 0; - for (int i = 0, n = row.size() - 1; i < n; i++) { - width += row.get(i); - cnt.merge(width, 1, Integer::sum); + for (var row : wall) { + int s = 0; + for (int i = 0; i + 1 < row.size(); ++i) { + s += row.get(i); + cnt.merge(s, 1, Integer::sum); } } - int max = cnt.values().stream().max(Comparator.naturalOrder()).orElse(0); - return wall.size() - max; + int mx = 0; + for (var x : cnt.values()) { + mx = Math.max(mx, x); + } + return wall.size() - mx; } } ``` +#### C++ + +```cpp +class Solution { +public: + int leastBricks(vector>& wall) { + unordered_map cnt; + for (const auto& row : wall) { + int s = 0; + for (int i = 0; i + 1 < row.size(); ++i) { + s += row[i]; + cnt[s]++; + } + } + int mx = 0; + for (const auto& [_, x] : cnt) { + mx = max(mx, x); + } + return wall.size() - mx; + } +}; +``` + #### Go ```go func leastBricks(wall [][]int) int { - cnt := make(map[int]int) + cnt := map[int]int{} for _, row := range wall { - width := 0 - for _, brick := range row[:len(row)-1] { - width += brick - cnt[width]++ + s := 0 + for _, x := range row[:len(row)-1] { + s += x + cnt[s]++ } } - max := 0 - for _, v := range cnt { - if v > max { - max = v - } + mx := 0 + for _, x := range cnt { + mx = max(mx, x) } - return len(wall) - max + return len(wall) - mx +} +``` + +#### TypeScript + +```ts +function leastBricks(wall: number[][]): number { + const cnt: Map = new Map(); + for (const row of wall) { + let s = 0; + for (let i = 0; i + 1 < row.length; ++i) { + s += row[i]; + cnt.set(s, (cnt.get(s) || 0) + 1); + } + } + const mx = Math.max(...cnt.values(), 0); + return wall.length - mx; } ``` @@ -127,17 +174,14 @@ func leastBricks(wall [][]int) int { var leastBricks = function (wall) { const cnt = new Map(); for (const row of wall) { - let width = 0; - for (let i = 0, n = row.length - 1; i < n; ++i) { - width += row[i]; - cnt.set(width, (cnt.get(width) || 0) + 1); + let s = 0; + for (let i = 0; i + 1 < row.length; ++i) { + s += row[i]; + cnt.set(s, (cnt.get(s) || 0) + 1); } } - let max = 0; - for (const v of cnt.values()) { - max = Math.max(max, v); - } - return wall.length - max; + const mx = Math.max(...cnt.values(), 0); + return wall.length - mx; }; ``` diff --git a/solution/0500-0599/0554.Brick Wall/README_EN.md b/solution/0500-0599/0554.Brick Wall/README_EN.md index 41253ff148ab2..b7ceb9703130e 100644 --- a/solution/0500-0599/0554.Brick Wall/README_EN.md +++ b/solution/0500-0599/0554.Brick Wall/README_EN.md @@ -56,7 +56,15 @@ tags: -### Solution 1 +### Solution 1: Hash Table + Prefix Sum + +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. + +Traverse each row, and for each brick in the current row, add it to the current prefix sum and update $\textit{cnt}$. + +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. + +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. @@ -65,15 +73,13 @@ tags: ```python class Solution: def leastBricks(self, wall: List[List[int]]) -> int: - cnt = defaultdict(int) + cnt = Counter() for row in wall: - width = 0 - for brick in row[:-1]: - width += brick - cnt[width] += 1 - if not cnt: - return len(wall) - return len(wall) - cnt[max(cnt, key=cnt.get)] + s = 0 + for x in row[:-1]: + s += x + cnt[s] += 1 + return len(wall) - max(cnt.values(), default=0) ``` #### Java @@ -82,38 +88,79 @@ class Solution: class Solution { public int leastBricks(List> wall) { Map cnt = new HashMap<>(); - for (List row : wall) { - int width = 0; - for (int i = 0, n = row.size() - 1; i < n; i++) { - width += row.get(i); - cnt.merge(width, 1, Integer::sum); + for (var row : wall) { + int s = 0; + for (int i = 0; i + 1 < row.size(); ++i) { + s += row.get(i); + cnt.merge(s, 1, Integer::sum); } } - int max = cnt.values().stream().max(Comparator.naturalOrder()).orElse(0); - return wall.size() - max; + int mx = 0; + for (var x : cnt.values()) { + mx = Math.max(mx, x); + } + return wall.size() - mx; } } ``` +#### C++ + +```cpp +class Solution { +public: + int leastBricks(vector>& wall) { + unordered_map cnt; + for (const auto& row : wall) { + int s = 0; + for (int i = 0; i + 1 < row.size(); ++i) { + s += row[i]; + cnt[s]++; + } + } + int mx = 0; + for (const auto& [_, x] : cnt) { + mx = max(mx, x); + } + return wall.size() - mx; + } +}; +``` + #### Go ```go func leastBricks(wall [][]int) int { - cnt := make(map[int]int) + cnt := map[int]int{} for _, row := range wall { - width := 0 - for _, brick := range row[:len(row)-1] { - width += brick - cnt[width]++ + s := 0 + for _, x := range row[:len(row)-1] { + s += x + cnt[s]++ } } - max := 0 - for _, v := range cnt { - if v > max { - max = v - } + mx := 0 + for _, x := range cnt { + mx = max(mx, x) } - return len(wall) - max + return len(wall) - mx +} +``` + +#### TypeScript + +```ts +function leastBricks(wall: number[][]): number { + const cnt: Map = new Map(); + for (const row of wall) { + let s = 0; + for (let i = 0; i + 1 < row.length; ++i) { + s += row[i]; + cnt.set(s, (cnt.get(s) || 0) + 1); + } + } + const mx = Math.max(...cnt.values(), 0); + return wall.length - mx; } ``` @@ -127,17 +174,14 @@ func leastBricks(wall [][]int) int { var leastBricks = function (wall) { const cnt = new Map(); for (const row of wall) { - let width = 0; - for (let i = 0, n = row.length - 1; i < n; ++i) { - width += row[i]; - cnt.set(width, (cnt.get(width) || 0) + 1); + let s = 0; + for (let i = 0; i + 1 < row.length; ++i) { + s += row[i]; + cnt.set(s, (cnt.get(s) || 0) + 1); } } - let max = 0; - for (const v of cnt.values()) { - max = Math.max(max, v); - } - return wall.length - max; + const mx = Math.max(...cnt.values(), 0); + return wall.length - mx; }; ``` diff --git a/solution/0500-0599/0554.Brick Wall/Solution.cpp b/solution/0500-0599/0554.Brick Wall/Solution.cpp new file mode 100644 index 0000000000000..426dbdc6e49e3 --- /dev/null +++ b/solution/0500-0599/0554.Brick Wall/Solution.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int leastBricks(vector>& wall) { + unordered_map cnt; + for (const auto& row : wall) { + int s = 0; + for (int i = 0; i + 1 < row.size(); ++i) { + s += row[i]; + cnt[s]++; + } + } + int mx = 0; + for (const auto& [_, x] : cnt) { + mx = max(mx, x); + } + return wall.size() - mx; + } +}; diff --git a/solution/0500-0599/0554.Brick Wall/Solution.go b/solution/0500-0599/0554.Brick Wall/Solution.go index a23a5f6459b94..18cee31c05524 100644 --- a/solution/0500-0599/0554.Brick Wall/Solution.go +++ b/solution/0500-0599/0554.Brick Wall/Solution.go @@ -1,17 +1,15 @@ func leastBricks(wall [][]int) int { - cnt := make(map[int]int) + cnt := map[int]int{} for _, row := range wall { - width := 0 - for _, brick := range row[:len(row)-1] { - width += brick - cnt[width]++ + s := 0 + for _, x := range row[:len(row)-1] { + s += x + cnt[s]++ } } - max := 0 - for _, v := range cnt { - if v > max { - max = v - } + mx := 0 + for _, x := range cnt { + mx = max(mx, x) } - return len(wall) - max -} \ No newline at end of file + return len(wall) - mx +} diff --git a/solution/0500-0599/0554.Brick Wall/Solution.java b/solution/0500-0599/0554.Brick Wall/Solution.java index e4447835bcbbf..f3651533b2e4b 100644 --- a/solution/0500-0599/0554.Brick Wall/Solution.java +++ b/solution/0500-0599/0554.Brick Wall/Solution.java @@ -1,14 +1,17 @@ class Solution { public int leastBricks(List> wall) { Map cnt = new HashMap<>(); - for (List row : wall) { - int width = 0; - for (int i = 0, n = row.size() - 1; i < n; i++) { - width += row.get(i); - cnt.merge(width, 1, Integer::sum); + for (var row : wall) { + int s = 0; + for (int i = 0; i + 1 < row.size(); ++i) { + s += row.get(i); + cnt.merge(s, 1, Integer::sum); } } - int max = cnt.values().stream().max(Comparator.naturalOrder()).orElse(0); - return wall.size() - max; + int mx = 0; + for (var x : cnt.values()) { + mx = Math.max(mx, x); + } + return wall.size() - mx; } -} \ No newline at end of file +} diff --git a/solution/0500-0599/0554.Brick Wall/Solution.js b/solution/0500-0599/0554.Brick Wall/Solution.js index 078f4d90b35b3..a30882bd06b41 100644 --- a/solution/0500-0599/0554.Brick Wall/Solution.js +++ b/solution/0500-0599/0554.Brick Wall/Solution.js @@ -5,15 +5,12 @@ var leastBricks = function (wall) { const cnt = new Map(); for (const row of wall) { - let width = 0; - for (let i = 0, n = row.length - 1; i < n; ++i) { - width += row[i]; - cnt.set(width, (cnt.get(width) || 0) + 1); + let s = 0; + for (let i = 0; i + 1 < row.length; ++i) { + s += row[i]; + cnt.set(s, (cnt.get(s) || 0) + 1); } } - let max = 0; - for (const v of cnt.values()) { - max = Math.max(max, v); - } - return wall.length - max; + const mx = Math.max(...cnt.values(), 0); + return wall.length - mx; }; diff --git a/solution/0500-0599/0554.Brick Wall/Solution.py b/solution/0500-0599/0554.Brick Wall/Solution.py index cfd160aa3d1f3..5d31c91acdb35 100644 --- a/solution/0500-0599/0554.Brick Wall/Solution.py +++ b/solution/0500-0599/0554.Brick Wall/Solution.py @@ -1,11 +1,9 @@ class Solution: def leastBricks(self, wall: List[List[int]]) -> int: - cnt = defaultdict(int) + cnt = Counter() for row in wall: - width = 0 - for brick in row[:-1]: - width += brick - cnt[width] += 1 - if not cnt: - return len(wall) - return len(wall) - cnt[max(cnt, key=cnt.get)] + s = 0 + for x in row[:-1]: + s += x + cnt[s] += 1 + return len(wall) - max(cnt.values(), default=0) diff --git a/solution/0500-0599/0554.Brick Wall/Solution.ts b/solution/0500-0599/0554.Brick Wall/Solution.ts new file mode 100644 index 0000000000000..2bb46679dc858 --- /dev/null +++ b/solution/0500-0599/0554.Brick Wall/Solution.ts @@ -0,0 +1,12 @@ +function leastBricks(wall: number[][]): number { + const cnt: Map = new Map(); + for (const row of wall) { + let s = 0; + for (let i = 0; i + 1 < row.length; ++i) { + s += row[i]; + cnt.set(s, (cnt.get(s) || 0) + 1); + } + } + const mx = Math.max(...cnt.values(), 0); + return wall.length - mx; +}