Brick Wall
There is a rectangular brick wall in front of you with nith
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
Example 1
Output: 2
Explanation: If we visualize the
wall
Example 2
Output: 3
Contents
Solution 1 - At every brick unit, count number of cuts needed from top row to bottom row (Brute force approach)
Per the problem statement it is clear that every row will have same width though each brick width may be different.
So, in this approach we are going to get the width of the row as a first step, then at every single brick unitHashMap
Finally, return the minimum value stored in the HashMap
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class BrickWall {
static int bruteForceApproach(List<List<Integer>> wall) {
HashMap<Integer, Integer> map = new HashMap<>();
// first get the width
int width = 0;
List<Integer> first_row = wall.get(0);
for(int brick : first_row) {
width += brick;
}
if(width == 1) {
return wall.size();
}
for(List<Integer> row: wall) {
int brickUnits=1;
int currentWidth =row.get(0);
int i=1;
while(brickUnits < width){
if(currentWidth > brickUnits) {
map.merge(brickUnits, 1, Integer::sum);
} else {
map.merge(brickUnits, 0, Integer::sum);
currentWidth += row.get(i);
i++;
}
brickUnits++;
}
}
// get the minimum cut point, min value from HashMap
int min = Integer.MAX_VALUE;
for(Integer val: map.values()) {
if(val < min) {
min = val;
}
}
return min;
}
public static void main(String[] args) {
List<List<Integer>> wall = List.of(
List.of(1,2,2,1),
List.of(3,1,2),
List.of(1,3,2),
List.of(2,4),
List.of(3,1,2),
List.of(1,3,1,1)
);
System.out.println(bruteForceApproach(wall));
wall = List.of(
List.of(1),
List.of(1),
List.of(1)
);
System.out.println(bruteForceApproach(wall));
}
}
Complexity Analysis:
Time complexity: Above code runs in O(W * H) time where WH
Space complexity: O(W), since we are storing every brick unit into HashMap
Solution 2 - Find the gaps count at brick position and find the position with maximum gaps, that means less cuts (Efficient approach)
In this approach, we will count the gaps at brick position in every row and store this into a HashMap
Implementation details:
-
We will first initialize a
to store the brick position asHashMap and number of gaps found askey .value -
Then loop through each
of wall, and store the brick position (i.e. width of current brick) asrow and putkey as value if the key doesn't exist, otherwise increment the value.1Note: When we loop through each
, we will ignore last brick, because we cannot include edge positions as per problem statement.row -
Now, get the maximum number of gaps found, by going through values of
and get the maximum value.HashMap -
Finally subtract the maximum number of gaps found from
, this will give minimum number of cuts needed to reach from top row to bottom row.wall height
Let's look at an example, say if the given input wall is [[1,2,2,1],[3,1,2],[1,3,2],[2,4],[3,1,2],[1,3,1,1]]
-
After going through first row
, gaps map will look like this.[1, 2, 2, 1]
Gaps Position Gaps 1 1 3 1 5 1 -
After going through second row
, gaps map will look like this.[3, 1, 2]Gaps Position Gaps 1 1 3 2 4 1 5 1 -
After going through third row
, gaps map will look like this.[1, 3, 2]Gaps Position Gaps 1 2 3 2 4 2 5 1 -
After going through fourth row
, gaps map will look like this.[2, 4]Gaps Position Gaps 1 2 2 1 3 2 4 2 5 1 -
After going through fifth row
, gaps map will look like this.[3, 1, 2]Gaps Position Gaps 1 2 2 1 3 3 4 3 5 1 -
After going through sixth row
, gaps map will look like this.[1, 3, 1, 1]Gaps Position Gaps 1 3 2 1 3 3 4 4 5 2 -
As we can see the maximum number of gaps found are
, we will subtract this from wall height which is4 . So we need to cut a minimum of6 -4 = 2 bricks to reach from top row to bottom row.2
import java.util.HashMap;
import java.util.List;
public class BrickWall {
static int countGapsAndFindMaxGaps(List<List<Integer>> wall) {
final HashMap<Integer, Integer> gaps = new HashMap<>();
for(List<Integer> row: wall) {
int currentBrickPosition = 0;
// we will be ignoring the last brick, because we cannot include edge positions per problem statement
for(int i=0; i<row.size()-1; i++) {
currentBrickPosition += row.get(i);
gaps.merge(currentBrickPosition, 1, Integer::sum);
}
}
int maxGaps = 0;
for(Integer gap : gaps.values()) {
if(gap>maxGaps) {
maxGaps = gap;
}
}
return wall.size() - maxGaps;
}
public static void main(String[] args) {
List<List<Integer>> wall_1 = List.of(
List.of(1,2,2,1),
List.of(3,1,2),
List.of(1,3,2),
List.of(2,4),
List.of(3,1,2),
List.of(1,3,1,1)
);
List<List<Integer>> wall_2 = List.of(
List.of(1),
List.of(1),
List.of(1)
);
System.out.println(countGapsAndFindMaxGaps(wall_1));
System.out.println(countGapsAndFindMaxGaps(wall_2));
}
}
Complexity Analysis:
Time complexity: Above code runs in O(m *n) time where mn
Space complexity: O(m) since we are storing position index to gap count into a HashMap
Above implementations source code can be found at