我已经实施了一个简短的算法,可以判断所有酒店预订是否可行。问题与Keywords and Reserved Words非常相似,但我试图实现自己的逻辑。
(非常抱歉这么久的解释)
在我的算法中
int
K =可用房间数ArrayList<Integer>
到达=来宾的到达时间ArrayList<Integer>
离开=客人离开时间int
arrival_guest =下一位客人ArrayList<ArrayList<Integer>>
安排=房间分配例如
现在,在我们开始迭代之前, ArrayList<ArrayList<Integer>> arrange
将是[[2]]
,因为最初所有房间都可用,int arriving_guest = 1
当第一位客人入住时,它将指向下一位客人< /强>
现在进行第一次迭代,其中客人的到达时间为3,出发时间为6
arrange
将成为[[2,6]]
arriving_guest
以指向下一位客人现在进行第二次迭代,其中客人的到达时间为5,出发时间为8
arrange
将成为[[2,6],[8]]
arriving_guest
以指向下一位客人在算法结束时
所需房间= arrange.size()
= 2
可用房间= K
= 1
因此它会返回上面的false
值
如果我将算法应用于以下测试用例,则无法
我的算法是
private static boolean bookingsPossible(ArrayList<Integer> arrive, ArrayList<Integer> depart, int K) {
// TODO Auto-generated method stub
int arriving_guest = 1;
ArrayList<ArrayList<Integer>> arrange = new ArrayList<ArrayList<Integer>>();
arrange.add(new ArrayList<Integer>(){{
add(depart.get(0));
}});
for(int i=0;i<depart.size();i++)
{
int temp = 0;
System.out.println("i is "+i);
while(temp <= i)
{
System.out.println("arriving guest "+arriving_guest);
if(arriving_guest < arrive.size() && arriving_guest < depart.size())
{
System.out.println("i is in if "+i);
if(temp <= (arrange.size() -1))
{
System.out.println("Temp is "+temp);
int vacatetime = arrange.get(temp).get(arrange.get(temp).size() - 1);
System.out.println("Vacate Time "+vacatetime+" Arrival Time "+arrive.get(arriving_guest));
if(vacatetime <= arrive.get(arriving_guest))
{
arrange.get(temp).add(depart.get(arriving_guest));
arriving_guest++;
System.out.println("Arrangement made "+arrange.toString());
break;
}
}
else
{
System.out.println("Adding room");
ArrayList<Integer> p = new ArrayList<Integer>();
p.add(depart.get(arriving_guest));
arrange.add(p);
System.out.println("Arrangement made "+arrange.toString());
arriving_guest++; break;
}
}
else
{
break;
}
temp++;
}
}
System.out.println("Rooms needed "+arrange.size());
System.out.println("Final Arrangement made "+arrange.toString());
return arrange.size() <= K ? true : false;
}
答案 0 :(得分:0)
如果我们对问题进行概括,则变为以下:给定间隔(a_i,b_i),找到最大数量的分段交叉点。
算法如下:
以下是算法的实现:
public class HotelBooking {
private static class Pair implements Comparable<Pair>{
final int x;
final boolean isEnd;
public Pair(int x, boolean isEnd) {
this.x = x;
this.isEnd = isEnd;
}
@Override
public int compareTo(Pair o) {
int cmp = this.x - o.x;
//in case of tie be sure that end pair comes first
return cmp == 0 ? isEnd ? -1 : 1 : cmp;
}
}
public boolean hotel(ArrayList<Integer> arrive, ArrayList<Integer> depart, int K) {
if(arrive.size() == 0 || K == 0) return false; //base case
else if(arrive.size() == 1 && K > 1) return false; //base case
else {
int n = depart.size();
List<Pair> intervals = new ArrayList<>();
for (int i = 0; i < n; i++) {
intervals.add(new Pair(arrive.get(i), false));
intervals.add(new Pair(depart.get(i), true));
}
Collections.sort(intervals);
int count = 0; //
int maxRooms = 0;
for (int i = 0; i < intervals.size(); i++) {
Pair pair = intervals.get(i);
count += pair.isEnd ? -1 : 1;
maxRooms = Math.max(maxRooms, count);
}
return maxRooms > K ? false : true;
}
}
}