📌  相关文章
📜  查找是否可以在给定的到达和离开时间进行k笔预订

📅  最后修改于: 2021-04-26 07:22:11             🧑  作者: Mango

旅馆经理必须在下个季节处理N个房间的提前预订。他的酒店有K间客房。预订包含到达日期和离开日期。他想找出酒店中是否有足够的房间来满足需求。

这个想法是对数组进行排序并跟踪重叠。

例子:

Input :  Arrivals :   [1 3 5]
         Departures : [2 6 8]
         K: 1
Output: False
Hotel manager needs at least two
rooms as the second and third 
intervals overlap.

方法1
这个想法是将辅助到达阵列中的到达和离开时间存储在一个带有附加标记的辅助数组中,以指示时间是到达还是离开。现在对数组进行排序。对于每个到达增量的活动预订,处理排序后的数组。对于每次离开,递减。跟踪最大活动预订。如果任何时候的活动预订数大于k,则返回false。否则返回true。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
bool areBookingsPossible(int arrival[],
                         int departure[], int n, int k)
{
    vector > ans;
 
    // create a common vector both arrivals
    // and departures.
    for (int i = 0; i < n; i++) {
        ans.push_back(make_pair(arrival[i], 1));
        ans.push_back(make_pair(departure[i], 0));
    }
 
    // sort the vector
    sort(ans.begin(), ans.end());
 
    int curr_active = 0, max_active = 0;
 
    for (int i = 0; i < ans.size(); i++) {
 
        // if new arrival, increment current
        // guests count and update max active
        // guests so far
        if (ans[i].second == 1) {
            curr_active++;
            max_active = max(max_active,
                             curr_active);
        }
 
        // if a guest departs, decrement
        // current guests count.
        else
            curr_active--;
    }
 
    // if max active guests at any instant
    // were more than the available rooms,
    // return false. Else return true.
    return (k >= max_active);
}
 
int main()
{
    int arrival[] = { 1, 3, 5 };
    int departure[] = { 2, 6, 8 };
    int n = sizeof(arrival) / sizeof(arrival[0]);
    cout << (areBookingsPossible(arrival,
                                 departure, n, 1)
                 ? "Yes\n"
                 : "No\n");
    return 0;
}


Java
// Java implementation of the above approach
import java.io.*;
import java.util.*;
 
// User defined Pair class
class Pair {
  int x;
  int y;
 
  // Constructor
  public Pair(int x, int y)
  {
    this.x = x;
    this.y = y;
  }
}
 
// class to define user defined conparator
class Compare {
 
  static void compare(Pair arr[], int n)
  {
 
    // Comparator to sort the pair according to second element
    Arrays.sort(arr, new Comparator() {
      @Override public int compare(Pair p1, Pair p2)
      {
        return p1.x - p2.x;
      }
    });
  }
}
 
class GFG
{
  static boolean areBookingsPossible(int arrival[],
                                     int departure[],
                                     int n, int k)
  {
    Pair ans[] = new Pair[2*n];
 
    // create a common vector both arrivals
    // and departures.
    int j = 0;
    for (int i = 0; i < n; i++)
    {
      ans[i + j] = new Pair(arrival[i], 1);
      ans[i + j + 1] = new Pair(departure[i], 0);
      j++;
    }
 
    // sort the vector
    Compare obj = new Compare();
    obj.compare(ans, 2 * n);     
    int curr_active = 0, max_active = 0;    
    for (int i = 0; i < 2 * n; i++)
    {
 
      // if new arrival, increment current
      // guests count and update max active
      // guests so far
      if (ans[i].y == 1)
      {
        curr_active++;
        max_active = Math.max(max_active,
                              curr_active);
      }
 
      // if a guest departs, decrement
      // current guests count.
      else
        curr_active--;
    }
 
    // if max active guests at any instant
    // were more than the available rooms,
    // return false. Else return true.
    return (k >= max_active);
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int arrival[] = { 1, 3, 5 };
    int departure[] = { 2, 6, 8 };
    int n = arrival.length;
    System.out.println(areBookingsPossible(arrival, departure, n, 1) ? "Yes" : "No");
  }
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 code for the above approach.
def areBookingsPossible(arrival, departure, n, k):
 
    ans = []
 
    # Create a common vector both arrivals
    # and departures.
    for i in range(0, n):
        ans.append((arrival[i], 1))
        ans.append((departure[i], 0))
 
    # Sort the vector
    ans.sort()
    curr_active, max_active = 0, 0
 
    for i in range(0, len(ans)):
 
        # If new arrival, increment current
        # guests count and update max active
        # guests so far
        if ans[i][1] == 1:
            curr_active += 1
            max_active = max(max_active,
                             curr_active)
 
        # if a guest departs, decrement
        # current guests count.
        else:
            curr_active -= 1
 
    # If max active guests at any instant
    # were more than the available rooms,
    # return false. Else return true.
    return k >= max_active
 
# Driver Code
if __name__ == "__main__":
 
    arrival = [1, 3, 5]
    departure = [2, 6, 8]
    n = len(arrival)
     
    if areBookingsPossible(arrival,
                           departure, n, 1):
        print("Yes")
    else:
        print("No")
     
# This code is contributed by Rituraj Jain


C++
// C++ code implementation of the above approach
#include 
using namespace std;
 
string areBookingsPossible(int A[], int B[],
                           int K, int N)
{
    sort(A, A + N);
    sort(B, B + N);
     
    for(int i = 0; i < N; i++)
    {
        if (i + K < N && A[i + K] < B[i])
        {
            return "No";
        }
    }
    return "Yes";
}
 
// Driver Code
int main()
{
    int arrival[] = { 1, 2, 3 };
    int departure[] = { 2, 3, 4 };
    int N = sizeof(arrival) / sizeof(arrival[0]);
    int K = 1;
   
    cout << (areBookingsPossible(
        arrival, departure, K, N));
 
    return 0;
}
 
// This code is contributed by rag2127


Java
// Java code implementation of the above approach
import java.util.*;
class GFG
{
      
static String areBookingsPossible(int []A, int []B,
                                  int K)
{
    Arrays.sort(A);
    Arrays.sort(B);  
    for(int i = 0; i < A.length; i++)
    {
        if (i + K < A.length && A[i + K] < B[i])
        {
            return "No";
        }
    }
    return "Yes";
}
  
// Driver Code
public static void main(String []s)
{
    int []arrival = { 1, 2, 3 };
    int []departure = { 2, 3, 4 };
    int K = 1;    
    System.out.print(areBookingsPossible(
        arrival, departure, K));
}
}
 
// This code is contributed by Pratham76


Python
# Python Code Implementation of the above approach
def areBookingsPossible(A, B, K):
        A.sort()
        B.sort()
        for i in range(len(A)):
            if i+K < len(A) and A[i+K] < B[i] :
                return "No"
        return "Yes"
 
if __name__ == "__main__":
    arrival = [1, 2, 3] 
    departure = [2, 3, 4]  
    K = 1
    print areBookingsPossible(arrival,departure,K)
 
# This code was contributed by Vidhi Modi


C#
// C# code implementation of the above approach
using System;
 
class GFG{
     
static string areBookingsPossible(int []A, int []B,
                                  int K)
{
    Array.Sort(A);
    Array.Sort(B);
     
    for(int i = 0; i < A.Length; i++)
    {
        if (i + K < A.Length && A[i + K] < B[i])
        {
            return "No";
        }
    }
    return "Yes";
}
 
// Driver Code
static void Main()
{
    int []arrival = { 1, 2, 3 };
    int []departure = { 2, 3, 4 };
    int K = 1;
     
    Console.Write(areBookingsPossible(
        arrival, departure, K));
}
}
 
// This code is contributed by rutvik_56


Javascript


输出
No

时间复杂度: O(n Log n)
辅助空间: O(n)

方法2
这个想法是先简单地对2个数组(到达日期的数组和出发日期的数组)进行排序。
现在,下一步将是检查一个特定范围内存在多少重叠。如果重叠的数量大于房间的数量,那么可以说我们容纳客人的房间就更少了。

因此,对于特定范围,其中到达日期(到达数组的第i个)是开始日期,离开日期(离开数组的第i个)是结束日期,只有在下一个到达日期(第i + 1个)是小于范围的结束日期且大于或等于范围的开始日期(由于这是一个排序数组,因此我们不必担心后者)。

考虑到我们已经对数组进行了排序,我们直接需要检查下一个Kth(i + Kth)到达日期是否在该范围内,如果确实如此,则该到达日期之前的所有日期也都将落在采用的范围内,导致K + 1与所讨论的范围重叠,因此超出了房间数。

以下是上述方法的实施–

C++

// C++ code implementation of the above approach
#include 
using namespace std;
 
string areBookingsPossible(int A[], int B[],
                           int K, int N)
{
    sort(A, A + N);
    sort(B, B + N);
     
    for(int i = 0; i < N; i++)
    {
        if (i + K < N && A[i + K] < B[i])
        {
            return "No";
        }
    }
    return "Yes";
}
 
// Driver Code
int main()
{
    int arrival[] = { 1, 2, 3 };
    int departure[] = { 2, 3, 4 };
    int N = sizeof(arrival) / sizeof(arrival[0]);
    int K = 1;
   
    cout << (areBookingsPossible(
        arrival, departure, K, N));
 
    return 0;
}
 
// This code is contributed by rag2127

Java

// Java code implementation of the above approach
import java.util.*;
class GFG
{
      
static String areBookingsPossible(int []A, int []B,
                                  int K)
{
    Arrays.sort(A);
    Arrays.sort(B);  
    for(int i = 0; i < A.length; i++)
    {
        if (i + K < A.length && A[i + K] < B[i])
        {
            return "No";
        }
    }
    return "Yes";
}
  
// Driver Code
public static void main(String []s)
{
    int []arrival = { 1, 2, 3 };
    int []departure = { 2, 3, 4 };
    int K = 1;    
    System.out.print(areBookingsPossible(
        arrival, departure, K));
}
}
 
// This code is contributed by Pratham76

Python

# Python Code Implementation of the above approach
def areBookingsPossible(A, B, K):
        A.sort()
        B.sort()
        for i in range(len(A)):
            if i+K < len(A) and A[i+K] < B[i] :
                return "No"
        return "Yes"
 
if __name__ == "__main__":
    arrival = [1, 2, 3] 
    departure = [2, 3, 4]  
    K = 1
    print areBookingsPossible(arrival,departure,K)
 
# This code was contributed by Vidhi Modi

C#

// C# code implementation of the above approach
using System;
 
class GFG{
     
static string areBookingsPossible(int []A, int []B,
                                  int K)
{
    Array.Sort(A);
    Array.Sort(B);
     
    for(int i = 0; i < A.Length; i++)
    {
        if (i + K < A.Length && A[i + K] < B[i])
        {
            return "No";
        }
    }
    return "Yes";
}
 
// Driver Code
static void Main()
{
    int []arrival = { 1, 2, 3 };
    int []departure = { 2, 3, 4 };
    int K = 1;
     
    Console.Write(areBookingsPossible(
        arrival, departure, K));
}
}
 
// This code is contributed by rutvik_56

Java脚本


输出
Yes

时间复杂度: O(n Log n)
辅助空间: Python排序使用的O(n)