We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 571ef25 commit 50b5fb1Copy full SHA for 50b5fb1
C++/brick-wall.cpp
@@ -26,3 +26,22 @@ class Solution {
26
return (n-maxcount);
27
}
28
};
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