Skip to content

Commit

Permalink
Create 0554-Brick-Wall.cpp
Browse files Browse the repository at this point in the history
Create the C++solution as the same solution in the YouTube video "https://www.youtube.com/watch?v=Kkmv2h48ekw"
  • Loading branch information
MHamiid authored Jan 14, 2023
1 parent ceb0b45 commit 05f29a2
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions cpp/0554-Brick-Wall.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public:
int leastBricks(vector<vector<int>>& wall) {
unordered_map<int, int> wallGapCount; // <Position, Gap count>
wallGapCount[0] = 0; // To handle an edge case where this is no elements

for(int r = 0; r < wall.size(); r++)
{
int position = 0;
for(int b = 0; b < wall[r].size() - 1; b++)
{
position += wall[r][b];
wallGapCount[position] += 1;
}
}

return wall.size() - max_element(wallGapCount.begin(), wallGapCount.end(), &Solution::compare)->second; // Total number of rows - Max gap
}

private:
static bool compare(const pair<int, int>& a, const pair<int, int>& b)
{
return a.second < b.second;
}

};

0 comments on commit 05f29a2

Please sign in to comment.