Given a matrix bricks[][] denoting the start and end coordinates of a brick’s breadth from an arrangement of rectangular bricks in a two-dimensional space. A bullet can be shot up exactly vertically from different points along the X-axis. A brick with coordinates Xstart and Xend is penetrated by the bullet which is shot from position X if Xstart ? X ? Xend. There is no limit on the number of bullets that can be shot and a bullet once shot keeps traveling along Y-axis infinitely. The task is to find the minimum number of bullets that must be shot to penetrate all the bricks.
Examples:
Input: bricks[][] = {{10, 16}, {2, 8}, {1, 6}, {7, 12}}
Output: 2
Explanation:
Shoot one bullet at X = 6, it penetrates the bricks places at {2, 8} and {1, 6}
Another bullet at X = 11, it penetrates the bricks places at {7, 12} and {10, 16}
Input:bricks[][] = {{5000, 90000}, {150, 499}, {1, 100}}
Output: 3
Approach: The idea is to use Greedy technique. Follow the steps below to solve this problem:
- Initialize the required count of bullets with 0.
- Sort the Xstart and Xend on the basis of Xend in ascending order.
- Iterate over the sorted list of positions and check if the Xstart of the current brick is greater than or equal to the Xend of the previous brick. If so, then one more bullet is required. So increment the count by 1. Otherwise proceed.
- Return the count at the end.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool compare(vector< int >& a,
vector< int >& b)
{
return a[1] < b[1];
}
int findMinBulletShots(vector<vector< int > >& points)
{
sort(points.begin(), points.end(),
compare);
if (points.size() == 0)
return 0;
int cnt = 1;
int curr = points[0][1];
for ( int j = 1; j < points.size(); j++) {
if (curr < points[j][0]) {
cnt++;
curr = points[j][1];
}
}
return cnt;
}
int main()
{
vector<vector< int > > bricks{ { 5000, 900000 },
{ 1, 100 },
{ 150, 499 } };
cout << findMinBulletShots(bricks);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int findMinBulletShots( int [][] points)
{
Arrays.sort(points, (a, b) -> a[ 1 ] - b[ 1 ]);
if (points.length == 0 )
return 0 ;
int cnt = 1 ;
int curr = points[ 0 ][ 1 ];
for ( int j = 1 ; j < points.length; j++)
{
if (curr < points[j][ 0 ])
{
cnt++;
curr = points[j][ 1 ];
}
}
return cnt;
}
public static void main (String[] args)
{
int [][] bricks = { { 5000 , 900000 },
{ 1 , 100 },
{ 150 , 499 } };
System.out.print(findMinBulletShots(bricks));
}
}
|
Python3
def findMinBulletShots(points):
for i in range ( len (points)):
points[i] = points[i][:: - 1 ]
points = sorted (points)
for i in range ( len (points)):
points[i] = points[i][:: - 1 ]
if ( len (points) = = 0 ):
return 0
cnt = 1
curr = points[ 0 ][ 1 ]
for j in range ( 1 , len (points)):
if (curr < points[j][ 0 ]):
cnt + = 1
curr = points[j][ 1 ]
return cnt
if __name__ = = '__main__' :
bricks = [ [ 5000 , 900000 ],
[ 1 , 100 ],
[ 150 , 499 ] ]
print (findMinBulletShots(bricks))
|
Javascript
<script>
function compare(a,b)
{
return a[1] - b[1];
}
function findMinBulletShots(points)
{
points.sort(compare);
if (points.length == 0)
return 0;
let cnt = 1;
let curr = points[0][1];
for (let j = 1; j < points.length; j++) {
if (curr < points[j][0]) {
cnt++;
curr = points[j][1];
}
}
return cnt;
}
let bricks = [ [ 5000, 900000 ],
[ 1, 100 ],
[ 150, 499 ] ];
document.write(findMinBulletShots(bricks), "</br>" );
</script>
|
C#
using System;
using System.Collections.Generic;
public class PointComparer : IComparer<List< int >>
{
public int Compare(List< int > a, List< int > b)
{
return a[1].CompareTo(b[1]);
}
}
public class Program
{
public static int FindMinBulletShots(List<List< int >> points)
{
points.Sort( new PointComparer());
if (points.Count == 0)
return 0;
int cnt = 1;
int curr = points[0][1];
for ( int j = 1; j < points.Count; j++)
{
if (curr < points[j][0])
{
cnt++;
curr = points[j][1];
}
}
return cnt;
}
public static void Main()
{
List<List< int >> bricks = new List<List< int >>()
{
new List< int >(){ 5000, 900000 },
new List< int >(){ 1, 100 },
new List< int >(){ 150, 499 }
};
Console.WriteLine(FindMinBulletShots(bricks));
}
}
|
Time Complexity: O(N * log N), where N is the number of bricks.
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!