📜  穿透所有砖块所需的最少子弹数

📅  最后修改于: 2021-09-03 14:49:11             🧑  作者: Mango

给定矩阵砖 [][]表示来自二维空间中矩形砖排列的砖宽度的开始和结束坐标。子弹可以从沿 X 轴的不同点精确垂直射出。如果X start ≤ X ≤ X end ,则从位置X射出的子弹会穿透坐标为X startX end 的砖块。可以发射的子弹数量没有限制,一次发射的子弹会沿着 Y 轴无限移动。任务是找到穿透所有砖块必须射出的最少子弹数。

例子:

方法:这个想法是使用贪婪技术。请按照以下步骤解决此问题:

  1. 用 0 初始化所需的子弹数。
  2. X开始X结束X结束的顺序进行升序排序。
  3. 迭代排序的位置列表并检查当前砖的X开始是否大于或等于前一个砖的X结束。如果是这样,那么还需要一颗子弹。因此将计数增加 1。否则继续。
  4. 最后返回计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
  
#include 
using namespace std;
  
// Custom comparator function
bool compare(vector& a,
             vector& b)
{
    return a[1] < b[1];
}
  
// Function to find the minimum number of
// bullets required to penetrate all bricks
int findMinBulletShots(vector >& points)
{
    // Sort the points in ascending order
    sort(points.begin(), points.end(),
         compare);
  
    // Check if there are no points
    if (points.size() == 0)
        return 0;
  
    int cnt = 1;
    int curr = points[0][1];
  
    // Iterate through all the points
    for (int j = 1; j < points.size(); j++) {
        if (curr < points[j][0]) {
  
            // Increase the count
            cnt++;
            curr = points[j][1];
        }
    }
  
    // Return the count
    return cnt;
}
  
// Driver Code
int main()
{
    // Given coordinates of bricks
    vector > bricks{ { 5000, 900000 },
                                 { 1, 100 },
                                 { 150, 499 } };
  
    // Function call
    cout << findMinBulletShots(bricks);
  
    return 0;
}


Java
// Java program for above approach
import java.util.*;
  
class GFG{
  
// Function to find the minimum number of 
// bullets required to penetrate all bricks 
static int findMinBulletShots(int[][] points) 
{ 
      
    // Sort the points in ascending order 
    Arrays.sort(points, (a, b) -> a[1] - b[1]);
      
    // Check if there are no points 
    if (points.length == 0) 
        return 0; 
  
    int cnt = 1; 
    int curr = points[0][1]; 
  
    // Iterate through all the points 
    for(int j = 1; j < points.length; j++) 
    { 
        if (curr < points[j][0])
        { 
              
            // Increase the count 
            cnt++; 
            curr = points[j][1]; 
        } 
    } 
  
    // Return the count 
    return cnt; 
} 
  
// Driver code
public static void main (String[] args)
{
      
    // Given coordinates of bricks 
    int[][] bricks = { { 5000, 900000 }, 
                       { 1, 100 }, 
                       { 150, 499 } }; 
      
    // Function call 
    System.out.print(findMinBulletShots(bricks)); 
}
}
  
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
  
# Function to find the minimum number of
# bullets required to penetrate all bricks
def findMinBulletShots(points):
  
    # Sort the points in ascending order
    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]
  
    # Check if there are no points
    if (len(points) == 0):
        return 0
  
    cnt = 1
    curr = points[0][1]
  
    # Iterate through all the points
    for j in range(1, len(points)):
        if (curr < points[j][0]):
  
            # Increase the count
            cnt += 1
            curr = points[j][1]
              
    # Return the count
    return cnt
  
# Driver Code
if __name__ == '__main__':
      
    # Given coordinates of bricks
    bricks = [ [ 5000, 900000 ],
               [ 1, 100 ],
               [ 150, 499 ] ]
  
    # Function call
    print(findMinBulletShots(bricks))
  
# This code is contributed by mohit kumar 29


输出:
3

时间复杂度: O(N * log N),其中 N 是砖块的数量。
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live