Brick Wall LeetCode solution in C++

1
class Solution {
2
public:
3
int leastBricks(vector<vector<int>> &wall) {
4
unordered_map<int, int> edge_frequency; // HashMap to store the number of
5
// common edges among the rows
6
int max_frequency = 0; // Variable to store the frequency of most occuring edge
7

8
for (int row = 0; row < wall.size(); row++) // Iterating through each row
9
{
10
int edge_postion = 0; // Variable to store different edge postion
11

12
for (int brick_no = 0; brick_no < wall[row].size() - 1;
13
brick_no++) // Iterating through each brick inside a row
14
{
15
int current_brick_length = wall[row][brick_no]; // Length of the current brick
16
edge_postion =
17
edge_postion + current_brick_length; // Next Edge Position = Previous Edge Position
18
// + Current Brick's Length
19
edge_frequency[edge_postion]++; // Incrementing the Frequency of just
20
// calculated Edge Postion
21
max_frequency = max(edge_frequency[edge_postion],
22
max_frequency); // Comparing the "Frequency of just calculated
23
// Edge Postion" with "Max Frequency seen till
24
// now" & storing whichever is greater.
25
}
26
}
27
return wall.size() - max_frequency; // returning (Number of Bricks Crossed
28
// by Line) i.e. (Number of Rows in Wall
29
// - Frequency of Most Occuring Edge)
30
}
31
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0