📜  涵盖所有间隔的最小行驶距离

📅  最后修改于: 2021-04-24 05:07:27             🧑  作者: 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