📜  覆盖所有区间的最小行驶距离

📅  最后修改于: 2021-10-23 08:20:24             🧑  作者: Mango

给定许多区间作为范围和我们的位置。我们需要找到到达这样一个同时覆盖所有间隔的点的最小距离。

例子:

Input : Intervals = [(0, 7), (2, 14), (4, 6)]
        Position = 3
Output : 1
We can reach position 4 by traveling
distance 1, at which all intervals will
be covered. So answer will be 1

Input : Intervals = [(1, 2), (2, 3), (3, 4)]
        Position = 2
Output : -1 
It is not possible to cover all intervals
at once at any point

Input : Intervals = [(1, 2), (2, 3), (1, 4)]
        Position = 2
Output : 0
All Intervals are covered at current 
position only so no need travel and
answer will be 0

All above examples are shown in below diagram.

我们可以通过只关注端点来解决这个问题。由于要求是通过到达一个点来覆盖所有区间,因此所有区间必须共享一个点才能存在答案。即使是最左边终点的区间也必须与最右边起点的区间重叠。
首先,我们从所有间隔中找到最右边的起点和最左边的终点。然后我们可以将我们的位置与这些点进行比较以获得如下解释的结果:

  1. 如果这个最右侧的起点位于最左侧终点的右侧,则不可能同时覆盖所有区间。 (如示例 2 中所示)
  2. 如果我们的位置在最右端和最左端之间的中间,则无需旅行,所有间隔都将仅由当前位置覆盖(如示例 3 所示)
  3. 如果我们的位置在两个点都左边,那么我们需要移动到最右边的起点,如果我们的位置在两个点都右边,那么我们需要移动到最左边的终点。

请参阅上图以了解这些情况。和第一个例子一样,最右边的起点是 4,最左边的终点是 6,所以我们需要从当前位置 3 到达 4 以覆盖所有区间。

请参阅下面的代码以更好地理解。

C++
// C++ program to find minimum distance to
// travel to cover all intervals
#include 
using namespace std;
 
//  structure to store an interval
struct Interval
{
    int start, end;
    Interval(int start, int end) : start(start),
                                       end(end)
    {}
};
 
//  Method returns minimum distance to travel
// to cover all intervals
int minDistanceToCoverIntervals(Interval intervals[],
                                       int N, int x)
{
    int rightMostStart = INT_MIN;
    int leftMostEnd = INT_MAX;
 
    //  looping over all intervals to get right most
    // start and left most end
    for (int i = 0; i < N; i++)
    {
        if (rightMostStart < intervals[i].start)
            rightMostStart = intervals[i].start;
 
        if (leftMostEnd > intervals[i].end)
            leftMostEnd = intervals[i].end;
    }
     
    int res;
 
    /*  if rightmost start > leftmost end then all
        intervals are not aligned and it is not
        possible to cover all of them  */
    if (rightMostStart > leftMostEnd)
        res = -1;
 
    //  if x is in between rightmoststart and
    // leftmostend then no need to travel any distance
    else if (rightMostStart <= x && x <= leftMostEnd)
        res = 0;
     
    //  choose minimum according to current position x
    else
        res = (x < rightMostStart) ? (rightMostStart - x) :
                                     (x - leftMostEnd);
     
    return res;
}
 
//  Driver code to test above methods
int main()
{
    int x = 3;
    Interval intervals[] = {{0, 7}, {2, 14}, {4, 6}};
    int N = sizeof(intervals) / sizeof(intervals[0]);
 
    int res = minDistanceToCoverIntervals(intervals, N, x);
    if (res == -1)
        cout << "Not Possible to cover all intervals\n";
    else
        cout << res << endl;
}


Java
// Java program to find minimum distance
// to travel to cover all intervals
import java.util.*;
 
class GFG{
     
// Structure to store an interval
static class Interval
{
    int start, end;
    Interval(int start, int end)
    {
        this.start = start;
        this.end = end;
    }
};
 
// Method returns minimum distance to
// travel to cover all intervals
static int minDistanceToCoverIntervals(Interval intervals[],
                                       int N, int x)
{
    int rightMostStart = Integer.MIN_VALUE;
    int leftMostEnd = Integer.MAX_VALUE;
     
    // Looping over all intervals to get
    // right most start and left most end
    for(int i = 0; i < N; i++)
    {
        if (rightMostStart < intervals[i].start)
            rightMostStart = intervals[i].start;
 
        if (leftMostEnd > intervals[i].end)
            leftMostEnd = intervals[i].end;
    }
     
    int res;
 
    // If rightmost start > leftmost end then
    // all intervals are not aligned and it
    // is not possible to cover all of them 
    if (rightMostStart > leftMostEnd)
        res = -1;
         
    // If x is in between rightmoststart and
    // leftmostend then no need to travel
    // any distance
    else if (rightMostStart <= x &&
             x <= leftMostEnd)
        res = 0;
     
    // Choose minimum according to
    // current position x
    else
        res = (x < rightMostStart) ?
              (rightMostStart - x) :
              (x - leftMostEnd);
     
    return res;
}
 
//  Driver code
public static void main(String[] args)
{
    int x = 3;
    Interval []intervals = { new Interval(0, 7),
                             new Interval(2, 14),
                             new Interval(4, 6) };
    int N = intervals.length;
 
    int res = minDistanceToCoverIntervals(
        intervals, N, x);
     
    if (res == -1)
        System.out.print("Not Possible to " +
                         "cover all intervals\n");
    else
        System.out.print(res + "\n");
}
}
 
// This code is contributed by Rajput-Ji


C#
// C# program to find minimum distance
// to travel to cover all intervals
using System;
 
class GFG{
     
// Structure to store an interval
public class Interval
{
    public int start, end;
     
    public Interval(int start, int end)
    {
        this.start = start;
        this.end = end;
    }
};
 
// Method returns minimum distance to
// travel to cover all intervals
static int minDistanceToCoverIntervals(
    Interval []intervals, int N, int x)
{
    int rightMostStart = int.MinValue;
    int leftMostEnd = int.MaxValue;
     
    // Looping over all intervals to get
    // right most start and left most end
    for(int i = 0; i < N; i++)
    {
        if (rightMostStart < intervals[i].start)
            rightMostStart = intervals[i].start;
 
        if (leftMostEnd > intervals[i].end)
            leftMostEnd = intervals[i].end;
    }
     
    int res;
 
    // If rightmost start > leftmost end then
    // all intervals are not aligned and it
    // is not possible to cover all of them 
    if (rightMostStart > leftMostEnd)
        res = -1;
         
    // If x is in between rightmoststart and
    // leftmostend then no need to travel
    // any distance
    else if (rightMostStart <= x &&
             x <= leftMostEnd)
        res = 0;
     
    // Choose minimum according to
    // current position x
    else
        res = (x < rightMostStart) ?
              (rightMostStart - x) :
              (x - leftMostEnd);
     
    return res;
}
 
//  Driver code
public static void Main(String[] args)
{
    int x = 3;
    Interval []intervals = { new Interval(0, 7),
                             new Interval(2, 14),
                             new Interval(4, 6) };
    int N = intervals.Length;
 
    int res = minDistanceToCoverIntervals(
        intervals, N, x);
     
    if (res == -1)
        Console.Write("Not Possible to " +
                      "cover all intervals\n");
    else
        Console.Write(res + "\n");
}
}
 
// This code is contributed by shikhasingrajput


输出:

1

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