-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0554-brick-wall.cpp
42 lines (33 loc) · 1.1 KB
/
0554-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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
Approach:
Store the count of the end of the brick for each row in a hash and keep the track
of max number of brick that ends at same position, return rows - max.
Time complexity : O(n x m)
Space complexity: O(n x m)
n is number of rows, m is max brick in a row.
*/
class Solution {
public:
int leastBricks(vector<vector<int>>& wall) {
map<int,int> end_count;
int end_of_brick, max_end_count=0;
int rows = wall.size(),cols;
for(int i =0;i<rows;i++){
end_of_brick = 0;
// '-1' because edge of the wall is not considered
cols = wall[i].size() -1;
for(int j =0;j<cols;j++){
end_of_brick += wall[i][j];
if(end_count.find(end_of_brick)!=end_count.end())
{
end_count[end_of_brick]++;
}
else{
end_count[end_of_brick] = 1;
}
max_end_count = max(max_end_count,end_count[end_of_brick]);
}
}
return rows - max_end_count;
}
};