酒店经理必须处理下个季节的 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.
方法一
这个想法是将到达和离开时间存储在一个辅助数组中,并带有一个额外的标记来指示时间是到达还是离开。现在对数组进行排序。对于每个到达增量活动预订,处理已排序的数组。每次离开,递减。跟踪最大活跃预订。如果任何时刻的活动预订数大于 k,则返回 false。否则返回真。
下面是上述方法的实现:
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
Javascript
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 个数组(到达日期的数组和出发日期的数组)进行排序。
现在,下一步将是检查一个特定范围内存在多少重叠。如果重叠的数量大于房间的数量,我们可以说我们有更少的房间来容纳客人。
因此,对于到达日期(第 i 个到达数组)作为开始日期和离开日期(第 i 个离开数组)作为结束日期的特定范围,只有在下一个到达日期(第 i+1 个)是时才可能重叠小于范围的结束日期并且大于或等于范围的开始日期(因为这是一个排序数组,我们不需要关心后一个条件)。
考虑到我们已经排序了数组,我们直接需要检查下第K(i+K)个到达日期是否在范围内,如果是,则该到达日期之前的所有日期也将在采取范围内,导致 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
Javascript
Yes
时间复杂度: O(n Log n)
辅助空间: Python排序使用的 O(n)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。