Skip to content

Commit 05f29a2

Browse files
authored
Create 0554-Brick-Wall.cpp
Create the C++solution as the same solution in the YouTube video "https://www.youtube.com/watch?v=Kkmv2h48ekw"
1 parent ceb0b45 commit 05f29a2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

cpp/0554-Brick-Wall.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int leastBricks(vector<vector<int>>& wall) {
4+
unordered_map<int, int> wallGapCount; // <Position, Gap count>
5+
wallGapCount[0] = 0; // To handle an edge case where this is no elements
6+
7+
for(int r = 0; r < wall.size(); r++)
8+
{
9+
int position = 0;
10+
for(int b = 0; b < wall[r].size() - 1; b++)
11+
{
12+
position += wall[r][b];
13+
wallGapCount[position] += 1;
14+
}
15+
}
16+
17+
return wall.size() - max_element(wallGapCount.begin(), wallGapCount.end(), &Solution::compare)->second; // Total number of rows - Max gap
18+
}
19+
20+
private:
21+
static bool compare(const pair<int, int>& a, const pair<int, int>& b)
22+
{
23+
return a.second < b.second;
24+
}
25+
26+
};

0 commit comments

Comments
 (0)