Minimum Number of Bullets required to penetrate all bricks | C ++ program for the above approach ; Custom comparator function ; Function to find the minimum number of bullets required to penetrate all bricks ; Sort the points in ascending order ; Check if there are no points ; Iterate through all the points ; Increase the count ; Return the count ; Driver Code ; Given coordinates of bricks ; Function call
#include <bits/stdc++.h> NEW_LINE 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 ; }