Brick Wall – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
There is a rectangular brick wall in front of you with n
rows of bricks. The ith
row has some number of bricks each of the same height (i.e., one unit) but they can be of different widths. The total width of each row is the same.
Draw a vertical line from the top to the bottom and cross the least bricks. If your line goes through the edge of a brick, then the brick is not considered as crossed. You cannot draw a line just along one of the two vertical edges of the wall, in which case the line will obviously cross no bricks.
Given the 2D array wall
that contains the information about the wall, return the minimum number of crossed bricks after drawing such a vertical line.
Example 1:
Input: wall = [[1,2,2,1],[3,1,2],[1,3,2],[2,4],[3,1,2],[1,3,1,1]] Output: 2
Example 2:
Input: wall = [[1],[1],[1]] Output: 3
Constraints:
n == wall.length
1 <= n <= 104
1 <= wall[i].length <= 104
1 <= sum(wall[i].length) <= 2 * 104
sum(wall[i])
is the same for each rowi
.1 <= wall[i][j] <= 231 - 1
C++ Brick Wall LeetCode Solution
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)
}
};
Java Brick Wall LeetCode Solution
class Solution {
public int leastBricks(List<List<Integer>> wall) {
Map<Integer, Integer> map = new HashMap();
int count = 0;
for (List<Integer> row : wall) {
int sum = 0;
for (int i = 0; i < row.size() - 1; i++) {
sum += row.get(i);
map.put(sum, map.getOrDefault(sum, 0) + 1);
count = Math.max(count, map.get(sum));
}
}
return wall.size() - count;
}
}
Python 3 Brick Wall LeetCode Solution
def leastBricks(self, wall: List[List[int]]) -> int:
edges, maxEdges = defaultdict(int), 0
for row in wall:
for idx in accumulate(row[:-1]):
edges[idx] += 1
maxEdges = max(maxEdges, edges[idx])
return len(wall) - maxEdges
Array-1180
String-562
Hash Table-412
Dynamic Programming-390
Math-368
Sorting-264
Greedy-257
Depth-First Search-256
Database-215
Breadth-First Search-200
Tree-195
Binary Search-191
Matrix-176
Binary Tree-160
Two Pointers-151
Bit Manipulation-140
Stack-133
Heap (Priority Queue)-117
Design-116
Graph-108
Simulation-103
Prefix Sum-96
Backtracking-92
Counting-86
Sliding Window-73
Linked List-69
Union Find-66
Ordered Set-48
Monotonic Stack-47
Recursion-43
Trie-41
Binary Search Tree-40
Divide and Conquer-40
Enumeration-39
Bitmask-37
Queue-33
Memoization-32
Topological Sort-31
Geometry-30
Segment Tree-27
Game Theory-24
Hash Function-24
Binary Indexed Tree-21
Interactive-18
Data Stream-17
String Matching-17
Rolling Hash-17
Shortest Path-16
Number Theory-16
Combinatorics-15
Randomized-12
Monotonic Queue-9
Iterator-9
Merge Sort-9
Concurrency-9
Doubly-Linked List-8
Brainteaser-8
Probability and Statistics-7
Quickselect-7
Bucket Sort-6
Suffix Array-6
Minimum Spanning Tree-5
Counting Sort-5
Shell-4
Line Sweep-4
Reservoir Sampling-4
Eulerian Circuit-3
Radix Sort-3
Strongly Connected Componen-t2
Rejection Sampling-2
Biconnected Component-1