-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathBrick Wall.cpp
24 lines (21 loc) · 1.4 KB
/
Brick Wall.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Runtime: 31 ms (Top 87.54%) | Memory: 21.90 MB (Top 67.02%)
class Solution {
public:
int leastBricks(vector<vector<int>>& wall)
{
unordered_map<int, int> edge_frequency; //HashMap to store the number of common edges among the rows
int max_frequency = 0; //Variable to store the frequency of most occuring edge
for(int row=0; row<wall.size(); row++) //Iterating through each row
{
int edge_postion = 0; //Variable to store different edge postion
for(int brick_no=0; brick_no< wall[row].size() -1; brick_no++) //Iterating through each brick inside a row
{
int current_brick_length = wall[row][brick_no]; //Length of the current brick
edge_postion = edge_postion + current_brick_length ; //Next Edge Position = Previous Edge Position + Current Brick's Length
edge_frequency[edge_postion]++; //Incrementing the Frequency of just calculated Edge Postion
max_frequency = max(edge_frequency[edge_postion],max_frequency); //Comparing the "Frequency of just calculated Edge Postion" with "Max Frequency seen till now" & storing whichever is greater.
}
}
return wall.size() - max_frequency; // returning (Number of Bricks Crossed by Line) i.e. (Number of Rows in Wall - Frequency of Most Occuring Edge)
}
};