Hotel Bytelandia

 This is a problem of CodeChef it's explanation is also provided in the site, and before reading the explanation part try this problem on your own and then move to the code implementation part. Just consider this problem as an everyday problem and try to visualize it as a real world implementation, that will help you. 

I had a problem while getting the previous entries of the guests in the starting but then I resolved it and it took me around 40 minutes to solve this problem completely.

It was fairly easy with Python and maybe I will solve this using C++14 too, we'll see that...Maybe!

Problem:-

A holiday weekend is coming up, and Hotel Bytelandia needs to find out if it has enough rooms to accommodate all potential guests. A number of guests have made reservations. Each reservation consists of an arrival time, and a departure time. The hotel management has hired you to calculate the maximum number of guests that will be at the hotel simultaneously. Note that if one guest arrives at the same time another leaves, they are never considered to be at the hotel simultaneously (see the second example).

Input

Input will begin with an integer T, the number of test cases. Each test case begins with an integer N, the number of guests. Two lines follow, each with exactly N positive integers. The i-th integer of the first line is the arrival time of the i-th guest, and the i-th integer of the second line is the departure time of the i-th guest (which will be strictly greater than the arrival time).

Output

For each test case, print the maximum number of guests that are simultaneously at the hotel.

Constraints

  • T≤100
  • N≤100
  • All arrival/departure times will be between 1 and 1000, inclusive

Sample 1:

Input
Output
3
3
1 2 3
4 5 6
5
1 2 3 4 5
2 3 4 5 6
7
13 6 5 8 2 10 12
19 18 6 9 9 11 15

So, we just have to give the maximum number of guests that are in the hotel at any point of time, which means we just need to maintain the count of guests. That's it, so I just keep a count variable and whenever a guest comes to hotel I increase it by 1, but, but, we need to check that which all guests have left the hotel before increasing the count by 1, so we just sort the arrival times and keep arrival and departure times altogther, so that we can access both at the same time and can check the departure time by comparing to the arrival time of the guest who has just entered the hotel. So, basically we will check the previous guests arrival and depar. times and if find the depar. time of any guest less than or equal to the arrival time of the current guest then just decrease the count, that guest has left the hotel. After every entry compare the count with a max variable so that we can get the maximum count of guests at any point of time simultaneously. That's it!! Peace.

Code Implementation:-

for _ in range(int(input())):
    n=int(input())
    ls1=list(map(int,input().split()))
    ls2=list(map(int,input().split()))
    ls=[[ls1[i],ls2[i]] for i in range(n)]
    count=0
    max_count=-10
    ls.sort(key=lambda x:x[0])
    # print(f"ls is {ls}")
    for i,l in enumerate(ls):
        for j in range(i):
            dep_time=ls[j][1]
            if(dep_time!=-1):
                if(dep_time<l[0]):
                    ls[j][1]=-1
                    count-=1
                elif(dep_time==l[0]):
                    ls[j][1]=-1
                    count-=1
        count+=1
        if(count>max_count):
            max_count=count
    print(max_count)

Comments

Popular Posts