📜  找出爆破所有气球所需的最少箭头数

📅  最后修改于: 2022-05-13 01:56:09.441000             🧑  作者: Mango

找出爆破所有气球所需的最少箭头数

给定一个大小为N的数组points[][] ,其中points[i]表示从points[i][0]points[i][1]的 X 坐标区域上的气球。 Y坐标无关紧要。所有的气球都需要被爆破。要使气球爆裂,可以在点(x, 0)处发射箭头,它垂直向上行进并爆破所有满足条件points[i][0] <= x <= points[i][1]的气球.任务是找到爆破所有气球所需的最少箭头数。

例子:

方法:给定的问题可以通过使用贪心方法找到彼此重叠的气球,以便箭头可以穿过所有这些气球并爆裂它们来解决给定的问题。为了达到最佳效果,请按照 X 坐标升序对数组进行排序。因此,现在考虑 2 个气球,如果第二个气球在第一个气球之前开始,那么它必须在第一个气球之后或在相同位置结束。

请按照以下步骤解决问题:

  • 使用比较器/lambda 表达式Arrays.sort(points, (a, b)-> Integer.compare(a[1], b[1]))根据气球的结束位置对数组进行排序。
  • 制作一个可变箭头并将其初始化为1 (至少需要一个箭头来爆破气球)
  • 创建一个变量end并用points[0][1]初始化它,它将存储第一个气球的结尾。
  • 使用变量i迭代范围[1, N)并执行以下步骤:
    • 检查下一个气球points[i][0]的开始是否小于结束变量end
    • 如果是,则继续迭代。
    • 否则,将箭头的计数增加1并将end的值设置为points[i][1]
  • 完成上述步骤后,打印箭头的值作为所需箭头的结果计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
bool cmp(vector a, vector b)
{
    return b[1] > a[1];
}
 
// Function to find the minimum count of
// arrows required to burst all balloons
int minArrows(vector> points)
{
   
    // To sort our array according
    // to end position of balloons
    sort(points.begin(), points.end(), cmp);
 
    // Initialize end variable with
    // the end of first balloon
    int end = points[0][1];
 
    // Initialize arrow with 1
    int arrow = 1;
 
    // Iterate through the entire
    // arrow of points
    for (int i = 1; i < points.size(); i++)
    {
 
        // If the start of ith balloon
        // <= end than do nothing
        if (points[i][0] <= end)
        {
            continue;
        }
 
        // if start of the next balloon
        // >= end of the first balloon
        // then increment the arrow
        else
        {
 
            // Update the new end
            end = points[i][1];
            arrow++;
        }
    }
 
    // Return the total count of arrows
    return arrow;
}
 
// Driver code
int main()
{
 
    vector> points = {{10, 16}, {2, 8}, {1, 6}, {7, 12}};
    cout << (minArrows(points));
    return 0;
}
 
// This code is contributed by Potta Lokesh


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to find the minimum count of
    // arrows required to burst all balloons
    public static int minArrows(int points[][])
    {
        // To sort our array according
        // to end position of balloons
        Arrays.sort(points,
                    (a, b) -> Integer.compare(a[1], b[1]));
 
        // Initialize end variable with
        // the end of first balloon
        int end = points[0][1];
 
        // Initialize arrow with 1
        int arrow = 1;
 
        // Iterate through the entire
        // arrow of points
        for (int i = 1; i < points.length; i++) {
 
            // If the start of ith balloon
            // <= end than do nothing
            if (points[i][0] <= end) {
                continue;
            }
 
            // if start of the next balloon
            // >= end of the first balloon
            // then increment the arrow
            else {
 
                // Update the new end
                end = points[i][1];
                arrow++;
            }
        }
 
        // Return the total count of arrows
        return arrow;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[][] points
            = { { 10, 16 }, { 2, 8 }, { 1, 6 }, { 7, 12 } };
 
        System.out.println(
            minArrows(points));
    }
}


Python3
# Python3 program for the above approach
 
# Function to find the minimum count of
# arrows required to burst all balloons
def minArrows(points):
 
    # To sort our array according
    # to end position of balloons
    points = sorted(points,  key = lambda x:x[1])
 
    # Initialize end variable with
    # the end of first balloon
    end = points[0][1];
 
    # Initialize arrow with 1
    arrow = 1;
 
    # Iterate through the entire
    # arrow of points
    for i in range (1, len(points)) :
 
        # If the start of ith balloon
          # <= end than do nothing
        if (points[i][0] <= end) :
            continue;
     
 
        # if start of the next balloon
        # >= end of the first balloon
        # then increment the arrow
        else :
 
            # Update the new end
            end = points[i][1]       
            arrow = arrow + 1
    
    # Return the total count of arrows
    return arrow;
 
# Driver Code
points = [[10, 16 ], [ 2, 8 ], [1, 6 ], [ 7, 12 ]]
print(minArrows(points))
   
# This code is contributed by AR_Gaurav


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG {
 
    // Function to find the minimum count of
    // arrows required to burst all balloons
    public static int minArrows(int[][] points)
    {
        // To sort our array according
        // to end position of balloons
        // Array.Sort(points, (a, b) => {return a[1] - b[1];});
        Comparer comparer = Comparer.Default;
        Array.Sort(points, (x,y) => comparer.Compare(x[1],y[1]));
         
 
        // Initialize end variable with
        // the end of first balloon
        int end = points[0][1];
 
        // Initialize arrow with 1
        int arrow = 1;
 
        // Iterate through the entire
        // arrow of points
        for (int i = 1; i < points.Length; i++) {
 
            // If the start of ith balloon
            // <= end than do nothing
            if (points[i][0] <= end) {
                continue;
            }
 
            // if start of the next balloon
            // >= end of the first balloon
            // then increment the arrow
            else {
 
                // Update the new end
                end = points[i][1];
                arrow++;
            }
        }
 
        // Return the total count of arrows
        return arrow;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int[][] points
            = { new int[] { 10, 16 }, new int[] { 2, 8 }, new int[]{ 1, 6 }, new int[]{ 7, 12 } };
 
        Console.Write(minArrows(points));
    }
}
 
// This code is contributed by saurabh_jaiswal.


Javascript


输出:
2

时间复杂度: O(N*log(N))
辅助空间: O(1)