📜  穿透所有积木所需的最小子弹数

📅  最后修改于: 2021-04-21 23:27:44             🧑  作者: Mango

给定一个矩阵bricks [] [] ,它根据二维空间中矩形砖的排列来表示砖的宽度的起点和终点坐标。子弹可以从X轴的不同点垂直垂直射出。如果X开始≤X≤X结束,从位置X射出的子弹会穿透坐标为X起点X终点的砖块。可以发射的子弹数量没有限制,一次发射的子弹可以无限地沿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)