给定一个二维数组segments[][] ,其中每个segment的形式为[L, R],表示(X, Y)坐标,任务是找到一个与最大segment数重叠的segment。
例子:
Input: segments[][] = {{1, 4}, {2, 3}, {3, 6}}
Output: {3, 6}
Explanation: Every segment overlaps with all other segments. Therefore, print any one of them.
Input: segments[][] = {{1, 2}, {3, 8}, {4, 5}, {6, 7}, {9, 10}}
Output: {3, 8}
Explanation: The segment {3, 8} overlaps {4, 5} and {6, 7}.
处理方法:按照以下步骤解决问题:
- 很显然,在一个段[currL,currR],剩余的部分的所有的“R”值,其小于“currL”和剩余分段的所有的“L”的值,其是除currR’更大不会算作答案。
- 将所有‘R’值存储在一个数组中并执行二分搜索以查找所有小于‘currL’的‘R’值,同样这样做以查找所有大于‘currR’的‘L’值。
- 遍历数组并在每次迭代时使用最大交点更新线段坐标。
- 打印具有最大交叉点的线段。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the segment which
// overlaps with maximum number of segments
void maxIntersection(int segments[][2], int N)
{
// 'L' & 'R' co-ordinates of all
// segments are stored in lvalues & rvalues
vector rvalues(N), lvalues(N);
// Assign co-ordinates
for (int i = 0; i < N; ++i) {
lvalues[i] = segments[i][0];
rvalues[i] = segments[i][1];
}
// Co-ordinate compression
sort(lvalues.begin(), lvalues.end());
sort(rvalues.begin(), rvalues.end());
// Stores the required segment
pair answer = { -1, -1 };
// Stores the current maximum
// number of intersections
int numIntersections = 0;
for (int i = 0; i < N; ++i) {
// Find number of 'R' coordinates
// which are less than the 'L'
// value of the current segment
int lesser
= lower_bound(rvalues.begin(), rvalues.end(),
segments[i][0])
- rvalues.begin();
// Find number of 'L' coordinates
// which are greater than the 'R'
// value of the current segment
int greater = max(
0, N
- (int)(upper_bound(lvalues.begin(),
lvalues.end(),
segments[i][1])
- lvalues.begin()));
// Segments excluding 'lesser' and
// 'greater' gives the number of
// intersections
if ((N - lesser - greater) >= numIntersections) {
answer = { segments[i][0], segments[i][1] };
// Update the current maximum
numIntersections = (N - lesser - greater);
}
}
// Print segment coordinates
cout << answer.first << " " << answer.second;
}
// Driver Code
int main()
{
// Given segments
int segments[][2] = { { 1, 4 }, { 2, 3 }, { 3, 6 } };
// Size of segments array
int N = sizeof(segments) / sizeof(segments[0]);
maxIntersection(segments, N);
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to find the segment which
// overlaps with maximum number of segments
static void maxIntersection(int segments[][], int N)
{
// 'L' & 'R' co-ordinates of all
// segments are stored in lvalues & rvalues
int []rvalues = new int[N];
int []lvalues = new int[N];
// Assign co-ordinates
for (int i = 0; i < N; ++i)
{
lvalues[i] = segments[i][0];
rvalues[i] = segments[i][1];
}
// Co-ordinate compression
Arrays.sort(lvalues);
Arrays.sort(rvalues);
// Stores the required segment
int []answer = { -1, -1 };
// Stores the current maximum
// number of intersections
int numIntersections = 0;
for (int i = 0; i < N; ++i)
{
// Find number of 'R' coordinates
// which are less than the 'L'
// value of the current segment
int lesser
= lower_bound(rvalues, 0,
segments.length,
segments[i][0]);
// Find number of 'L' coordinates
// which are greater than the 'R'
// value of the current segment
int greater = Math.max(
0, N-(upper_bound(lvalues, 0,
segments.length,
segments[i][1])));
// Segments excluding 'lesser' and
// 'greater' gives the number of
// intersections
if ((N - lesser - greater) >= numIntersections) {
answer = new int[]{ segments[i][0], segments[i][1] };
// Update the current maximum
numIntersections = (N - lesser - greater);
}
}
// Print segment coordinates
System.out.print(answer[0]+ " " + answer[1]);
}
static int lower_bound(int[] a, int low, int high, int element){
while(low < high){
int middle = low + (high - low)/2;
if(element > a[middle])
low = middle + 1;
else
high = middle;
}
return low;
}
static int upper_bound(int[] a, int low, int high, int element){
while(low < high){
int middle = low + (high - low)/2;
if(a[middle] > element)
high = middle;
else
low = middle + 1;
}
return low;
}
// Driver Code
public static void main(String[] args)
{
// Given segments
int segments[][] = { { 1, 4 }, { 2, 3 }, { 3, 6 } };
// Size of segments array
int N = segments.length;
maxIntersection(segments, N);
}
}
// This code is contributed by 29AjayKumar
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to find the segment which
// overlaps with maximum number of segments
static void maxIntersection(int [,]segments, int N)
{
// 'L' & 'R' co-ordinates of all
// segments are stored in lvalues & rvalues
int []rvalues = new int[N];
int []lvalues = new int[N];
// Assign co-ordinates
for (int i = 0; i < N; ++i)
{
lvalues[i] = segments[i,0];
rvalues[i] = segments[i,1];
}
// Co-ordinate compression
Array.Sort(lvalues);
Array.Sort(rvalues);
// Stores the required segment
int []answer = { -1, -1 };
// Stores the current maximum
// number of intersections
int numIntersections = 0;
for (int i = 0; i < N; ++i)
{
// Find number of 'R' coordinates
// which are less than the 'L'
// value of the current segment
int lesser
= lower_bound(rvalues, 0,
segments.GetLength(0),
segments[i,0]);
// Find number of 'L' coordinates
// which are greater than the 'R'
// value of the current segment
int greater = Math.Max(
0, N-(upper_bound(lvalues, 0,
segments.GetLength(0),
segments[i,1])));
// Segments excluding 'lesser' and
// 'greater' gives the number of
// intersections
if ((N - lesser - greater) >= numIntersections) {
answer = new int[]{ segments[i,0], segments[i,1] };
// Update the current maximum
numIntersections = (N - lesser - greater);
}
}
// Print segment coordinates
Console.Write(answer[0]+ " " + answer[1]);
}
static int lower_bound(int[] a, int low,
int high, int element)
{
while(low < high)
{
int middle = low + (high - low)/2;
if(element > a[middle])
low = middle + 1;
else
high = middle;
}
return low;
}
static int upper_bound(int[] a, int low,
int high, int element)
{
while(low < high)
{
int middle = low + (high - low)/2;
if(a[middle] > element)
high = middle;
else
low = middle + 1;
}
return low;
}
// Driver Code
public static void Main(String[] args)
{
// Given segments
int [,]segments = { { 1, 4 }, { 2, 3 }, { 3, 6 } };
// Size of segments array
int N = segments.GetLength(0);
maxIntersection(segments, N);
}
}
// This code is contributed by shikhasingrajput
Javascript
输出:
3 6
时间复杂度: O(N * log(N))
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。