📜  检查 N 条给定的线是否可以与 K 条垂直线相交

📅  最后修改于: 2021-10-26 06:31:17             🧑  作者: Mango

给定N个水平线表示由阵列位置[] [大小N,其中位置[I]]表示具有第i水平行x坐标-从位置[I] [0]位置[I] [1]和一个整数K,表示可以绘制的最大垂直线数,任务是检查N 条给定的线是否可以与最多K 条垂直线相交。

例子:

方法:这个想法是使用贪心的方法来解决这个问题,通过对positions[][]数组进行排序,并用1条线,交叉尽可能多的线等等。请按照以下步骤解决问题:

  • 按升序对数组position[][]进行排序。
  • 将变量ans初始化为1以存储答案,将r初始化为position[0][1]以存储终点直到特定点,其他水平线可以与给定的考虑垂直线相交。
  • 使用变量迭代范围[1, N] ,例如 I 并执行以下步骤:
    • 如果位置[I] [0]是小于r,r的值设置为最小R位置[I] [1]
    • 否则,将ans的值加1 ,并将r的值设置为position[i][1]。
  • 如果k更大 小于等于ans,则打印“YES” ,否则打印“NO”

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if it is possible
// to intersect n lines using k vertical lines
void findIfPossible(int n, int k,
                    vector > position)
{
 
    sort(position.begin(), position.end());
 
    int ans = 1;
 
    // Track the point till a particular
    // vertical line can intersect
    int r = position[0][1];
 
    // Iterate over the range
    for (int i = 1; i < n; i++) {
 
        if (position[i][0] <= r) {
 
            r = min(r, position[i][1]);
        }
 
        else {
 
            ans++;
 
            r = position[i][1];
        }
    }
    if (k >= ans)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
}
 
// Driver Code
int main()
{
 
    int n = 5;
    int k = 2;
    vector > position = {
        { 2, 5 }, { 4, 6 }, { 7, 16 }, { 9, 10 }, { 10, 17 }
    };
 
    findIfPossible(n, k, position);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG
{
 
  // Function to check if it is possible
  // to intersect n lines using k vertical lines
  static void findIfPossible(int n, int k,
                             int[][] position)
  {
 
    Arrays.sort(position, Comparator.comparingDouble(o -> o[0]));
 
    int ans = 1;
 
    // Track the point till a particular
    // vertical line can intersect
    int r = position[0][1];
 
    // Iterate over the range
    for (int i = 1; i < n; i++) {
 
      if (position[i][0] <= r) {
 
        r = Math.min(r, position[i][1]);
      }
 
      else {
 
        ans++;
 
        r = position[i][1];
      }
    }
    if (k >= ans)
      System.out.println("YES");
    else
      System.out.println("NO");
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int n = 5;
    int k = 2;
    int[][] position = {
      { 2, 5 }, { 4, 6 }, { 7, 16 }, { 9, 10 }, { 10, 17 }
    };
 
    findIfPossible(n, k, position);
  }
}
 
// This code is contributed by sanjoy_62.


Python3
# python program for the above approach
 
# Function to check if it is possible
# to intersect n lines using k vertical lines
def findIfPossible(n, k, position):
    position.sort()
    ans = 1
 
    # Track the point till a particular
    # vertical line can intersect
    r = position[0][1]
 
    # Iterate over the range
    for i in range(1, n):
        if (position[i][0] <= r):
            r = min(r, position[i][1])
        else:
            ans += 1
            r = position[i][1]
    if (k >= ans):
        print("YES")
    else:
        print("NO")
 
# Driver Code
n = 5
k = 2
position = [[2, 5], [4, 6], [7, 16], [9, 10], [10, 17]]
findIfPossible(n, k, position)
 
# This code is contributed by amreshkumar3


C#
// C# program for the above approach
using System;
 
class GFG{
     
static void Sort(int[,] arr)
{
    for(int i = 0; i < arr.GetLength(0); i++)
    {
        for(int j = arr.GetLength(1) - 1; j > 0; j--)
        {
            for(int k = 0; k < j; k++)
            {
                if (arr[i, k] > arr[i, k + 1])
                {
                    int myTemp = arr[i, k];
                    arr[i, k] = arr[i, k + 1];
                    arr[i, k + 1] = myTemp;
                }
            }
        }
    } 
}
 
// Function to check if it is possible
// to intersect n lines using k vertical lines
static void findIfPossible(int n, int k,
                           int[,] position)
{
    Sort(position);
     
    int ans = 1;
     
    // Track the point till a particular
    // vertical line can intersect
    int r = position[0, 1];
     
    // Iterate over the range
    for(int i = 1; i < n; i++)
    {
        if (position[i, 0] <= r)
        {
            r = Math.Min(r, position[i, 1]);
        }
        else
        {
            ans++;
            r = position[i, 1];
        }
    }
    if (k >= ans)
        Console.Write("YES");
    else
        Console.Write("NO");
}
 
// Driver Code
public static void Main(string[] args)
{
    int n = 5;
    int k = 2;
    int[,] position = { { 2, 5 }, { 4, 6 },
                        { 7, 16 }, { 9, 10 },
                        { 10, 17 } };
     
    findIfPossible(n, k, position);
}
}
 
// This code is contributed by code_hunt


Javascript


输出:
YES

时间复杂度: O(NlogN)
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程