📌  相关文章
📜  选定的最小点,以使通过它们的线段的移除清空给定数组

📅  最后修改于: 2021-05-17 18:31:22             🧑  作者: Mango

给定2D数组arr [] [] ,其中每行的格式为{start,end},表示X轴上每个线段的起点和终点。第一步,选择X轴上的一个点,然后删除通过该点的所有线段。任务是找到删除阵列中所有线段所需的此类点的最小数量。

例子:

方法:可以使用贪婪技术解决问题。请按照以下步骤解决问题:

  • 初始化一个变量,例如cntSteps,以计算删除所有线段所需的步骤总数。
  • 根据线段的终点对数组arr [] []进行排序。
  • 初始化变量,例如, Points = arr [0] [1]以存储X轴的点。
  • 遍历数组并检查arr [i] [0]的值是否大于Point 。如果确定为true,则将cntSteps的值增加1并更新Points = arr [i] [1]的值
  • 最后,打印cntSteps的值。
C++
// C++ program to implememnt
// the above approach
 
 
#include 
using namespace std;
 
 
// Comperator function
bool comp(vector &x, vector y)
{
    return x[1] < y[1];
}
 
 
// Function to count the minimum number of
// steps required to delete all the segments
int cntMinSteps(vector > &arr,
                                   int N)
{
     
 
    // Stores count of steps required
    // to delete all the line segments
    int cntSteps = 1;
     
 
    // Sort the array based on end points
    // of line segments
    sort(arr.begin(), arr.end(), comp);
     
 
    // Stores point on X-axis
    int Points = arr[0][1];
     
 
    // Traverse the array
    for(int i = 0; i < N; i++) {
         
 
        // If arr[1][0] is
        // greater than Points
        if(arr[i][0] > Points) {
             
 
            // Update cntSteps
            cntSteps++;
             
 
            // Update Points
            Points = arr[i][1];
        }
    }
     
    return cntSteps;
     
}
 
 
// Driver Code
int main() {
     
    vector > arr
       = { { 9, 15 }, { 3, 8 },
            { 1, 6 }, { 7, 12 },
                      { 5, 10 } };
                             
    int N = arr.size();
     
    cout<< cntMinSteps(arr, N);
    return 0;
}


Java
// Java program to implememnt
// the above approach
import java.util.*;
 
class GFG{
 
// Function to sort by column
public static void sortbyColumn(int arr[][],
                                int col)
{
     
    // Using built-in sort function Arrays.sort
    Arrays.sort(arr, new Comparator()
    {
        @Override
         
        // Compare values according to columns
        public int compare(final int[] entry1, 
                           final int[] entry2)
        {
             
            // To sort in descending order revert 
            // the '>' Operator
            if (entry1[col] > entry2[col])
                return 1;
            else
                return -1;
        }
    });  // End of function call sort().
}
 
// Function to count the minimum number of
// steps required to delete all the segments
static int cntMinSteps(int[][] arr, int N)
{
     
    // Stores count of steps required
    // to delete all the line segments
    int cntSteps = 1;
     
    // Sort the array based on end points
    // of line segments
    sortbyColumn(arr, 1);
     
    // Stores point on X-axis
    int Points = arr[0][1];
     
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // If arr[1][0] is
        // greater than Points
        if(arr[i][0] > Points)
        {
             
            // Update cntSteps
            cntSteps++;
             
            // Update Points
            Points = arr[i][1];
        }
    }
    return cntSteps;
}
 
// Driver Code
public static void main(String[] args)
{
    int[][] arr = { { 9, 15 }, { 3, 8 },
                    { 1, 6 }, { 7, 12 },
                    { 5, 10 } };
                             
    int N = arr.length;
     
    System.out.print(cntMinSteps(arr, N));
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program to implememnt
# the above approach
 
# Comperator function
def comp(x, y):
    return x[1] < y[1]
   
# Function to count the
# minimum number of steps
# required to delete all
# the segments
def cntMinSteps(arr, N):
   
    # Stores count of steps
    # required to delete all
    # the line segments
    cntSteps = 1
 
    # Sort the array based
    # on end points of line
    # segments
    arr.sort(reverse = False)   
 
    # Stores point on X-axis
    Points = arr[0][1]   
 
    # Traverse the array
    for i in range(N):
       
        # If arr[1][0] is
        # greater than Points
        if(arr[i][0] > Points):
           
            # Update cntSteps
            cntSteps += 1
             
            # Update Points
            Points = arr[i][1]
     
    return cntSteps
 
# Driver Code
if __name__ == '__main__':
   
    arr = [[9, 15], [3, 8],
           [1, 6], [7, 12],
           [5, 10]]           
    N = len(arr)
    print(cntMinSteps(arr, N))
 
# This code is contributed by bgangwar59


输出:
2

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