Skip to content

Commit 50b5fb1

Browse files
authored
Helped optimise the code (#162)
1 parent 571ef25 commit 50b5fb1

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

C++/brick-wall.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,22 @@ class Solution {
2626
return (n-maxcount);
2727
}
2828
};
29+
30+
Reduced Code for the given problem that reduces one for loop and also has less auxiliary space!
31+
32+
// Time: O(n), n is the total number of the bricks
33+
// Space: O(m), m is the total number different widths
34+
35+
class Solution {
36+
public:
37+
int leastBricks(vector<vector<int>>& wall) {
38+
unordered_map<int, int> widths;
39+
auto result = wall.size();
40+
for (const auto& row : wall) {
41+
for (auto i = 0, width = 0; i < row.size() - 1; ++i) {
42+
result = min(result, wall.size() - (++widths[width += row[i]]));
43+
}
44+
}
45+
return result;
46+
}
47+
};

0 commit comments

Comments
 (0)