Minimum number of bricks that can be intersected
Last Updated :
30 Jun, 2021
Given a 2D array arr[][], representing width of bricks of the same height present on a wall, the task is to find the minimum number of bricks that can be intersected by drawing a straight line from the top to the bottom of the wall.
Note: A line is said to intersect a brick if it passes through the brick and is non-intersecting if it is touching a brick’s boundary.
Examples:
Input: arr[][] = {{1, 2, 2, 1}, {3, 1, 2}, {1, 3, 2}, {2, 4}, {3, 1, 2}, {1, 3, 1, 1}}
Output: 2
Explanation: Considering the top left corner of the 2D array as the origin, the line is drawn at the coordinate x = 4 on the x-axis, such that it crosses the bricks on the 1st and 4th level, resulting in the minimum number of bricks crossed.
Input: arr[][] = {{1, 1, 1}}
Output: 0
Explanation: The line can be drawn at x = 1 or x = 2 coordinate on the x-axis such that it does not cross any brick resulting in the minimum number of bricks crossed.
Naive Approach: The simplest approach is to consider straight lines that can be drawn on every possible coordinate on the x-axis, along the width of the wall, considering x = 0 at the top left corner of the wall. Then, calculate the number of bricks inserted in each case and print the minimum count obtained.
Time Complexity: O(N * M) where N is the total number of bricks and M is the total width of the wall
Auxiliary Space: O(1)
Approach: To optimize the above approach, the idea is to store the number of bricks ending at a certain width on the x-axis in a hashmap, and then find the line where the most number of bricks end. After getting this count, subtract it from the total height of the wall to get the minimum number of bricks crossed.
Follow the steps below to solve the problem:
- Initialize a hashmap, M to store the number of bricks ending at a certain width on the x-axis.
- Traverse the array, arr using the variable i to store row index
- Initialize a variable, width as 0 to store the ending position.
- Store the size of the current row in a variable X.
- Iterate in the range [0, X-2] using the variable j
- Increment the value of width by arr[i][j] and increment the value of width in M by 1.
- Also, keep track of the most number of bricks ending at a certain width and store it in a variable, res.
- Subtract the value of res from the overall height of the wall and store it in a variable, ans.
- Print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void leastBricks(vector<vector< int > > wall)
{
unordered_map< int , int > map;
int res = 0;
for (vector< int > list : wall) {
int width = 0;
for ( int i = 0; i < list.size() - 1; i++) {
width += list[i];
map[width]++;
res = max(res, map[width]);
}
}
cout << wall.size() - res;
}
int main()
{
vector<vector< int > > arr{
{ 1, 2, 2, 1 }, { 3, 1, 2 },
{ 1, 3, 2 }, { 2, 4 },
{ 3, 1, 2 }, { 1, 3, 1, 1 }
};
leastBricks(arr);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
public class GFG
{
static void leastBricks(ArrayList<ArrayList<Integer> > wall)
{
HashMap<Integer, Integer> map = new HashMap<>();
int res = 0 ;
for (ArrayList<Integer> list : wall) {
int width = 0 ;
for ( int i = 0 ; i < list.size() - 1 ; i++) {
width += list.get(i);
map.put(width,
map.getOrDefault(width, 0 ) + 1 );
res = Math.max(res,
map.getOrDefault(width, 0 ));
}
}
System.out.println(wall.size() - res);
}
public static void main(String[] args)
{
ArrayList<ArrayList<Integer> > arr
= new ArrayList<>();
arr.add( new ArrayList<>(Arrays.asList( 1 , 2 , 2 , 1 )));
arr.add( new ArrayList<>(Arrays.asList( 3 , 1 , 2 )));
arr.add( new ArrayList<>(Arrays.asList( 1 , 3 , 2 )));
arr.add( new ArrayList<>(Arrays.asList( 2 , 4 )));
arr.add( new ArrayList<>(Arrays.asList( 3 , 1 , 2 )));
arr.add( new ArrayList<>(Arrays.asList( 1 , 3 , 1 , 1 )));
leastBricks(arr);
}
}
|
Python3
from collections import defaultdict
def leastBricks(wall):
map = defaultdict( int )
res = 0
for list in wall:
width = 0
for i in range ( len ( list ) - 1 ):
width + = list [i]
map [width] + = 1
res = max (res, map [width])
print ( len (wall) - res)
if __name__ = = "__main__" :
arr = [
[ 1 , 2 , 2 , 1 ], [ 3 , 1 , 2 ],
[ 1 , 3 , 2 ], [ 2 , 4 ],
[ 3 , 1 , 2 ], [ 1 , 3 , 1 , 1 ]
]
leastBricks(arr)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void leastBricks(List<List< int >> wall)
{
Dictionary< int ,
int > map = new Dictionary< int ,
int >();
int res = 0;
foreach (List< int > subList in wall)
{
int width = 0;
for ( int i = 0; i < subList.Count - 1; i++)
{
width += subList[i];
if (map.ContainsKey(width))
map[width]++;
else
map.Add(width, 1);
res = Math.Max(res, map[width]);
}
}
Console.Write(wall.Count-res);
}
public static void Main()
{
List<List< int >> myList = new List<List< int >>();
myList.Add( new List< int >{ 1, 2, 2, 1 });
myList.Add( new List< int >{ 3, 1, 2 });
myList.Add( new List< int >{ 1, 3, 2 });
myList.Add( new List< int >{ 2, 4 });
myList.Add( new List< int >{ 3, 1, 2 });
myList.Add( new List< int >{ 1, 3, 1, 1 });
leastBricks(myList);
}
}
|
Javascript
<script>
function leastBricks(wall) {
let map = new Map();
let res = 0;
for (let list of wall) {
let width = 0;
for (let i = 0; i < list.length - 1; i++) {
width += list[i];
if (map.has(width)) {
map.set(width, map.get(width) + 1);
} else {
map.set(width, 1)
}
res = Math.max(res, map.get(width));
}
}
document.write(wall.length - res);
}
let arr = [
[1, 2, 2, 1], [3, 1, 2],
[1, 3, 2], [2, 4],
[3, 1, 2], [1, 3, 1, 1]
];
leastBricks(arr);
</script>
|
Time Complexity: O(N) where N is the total number of bricks on the wall
Auxiliary Space: O(M) where M is the total width of the wall
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...