Hotel Floors

Easy
0/40
Average time to solve is 10m
profile
Contributed by
53 upvotes
Asked in companies
BarclaysWalmartInspirisys

Problem statement

You are the receptionist at a hotel which has 10 floors, numbered from 0 to 9 and each floor has 26 rooms named from ‘A’ to ‘Z’. Being a receptionist your task is to handle booking queries.

You get booking queries in the form of strings of size 3 where 1st character is ‘+’ means room is booked, or ‘-’ means room is freed. Second character represents the floor of the room i.e, ‘0’ to ‘9’. Third character represents the room name i.e, ‘A’ to ‘Z’.

On booking of each room you collect 1 coin from the customer. After the end of all the booking queries you have to count the number of coins you collected.

You may assume that the list describes a correct sequence of bookings in chronological order i.e., only free rooms can be booked, and only booked rooms can be freed.

For Example:-

Consider booking queries to be ["+1A", "+3E", "-1A", "+4F", "+1A", "-3E"]
+1A: Room A on the 1st floor is booked and you collected 1 coin.
+3E: Room E on the 3rd floor is booked and you collected 1 coin.
-1A: Room A on the 1st floor is freed.
+4F: Room F on the 4th floor is booked and you collected 1 coin.
+1A: Room A on the 1st floor is booked and you collected 1 coin.
-3E: Room E on the 3rd floor is freed.
So you collected 4 coins.
Detailed explanation ( Input/output format, Notes, Images )
Input format:
The first line of input contains an integer ‘T’ denoting the number of test cases.

The first line of each test case contains an integer ‘N’ representing the number of queries.

The second line of each test case contains N space-separated strings representing booking queries.
Output format :
For each test case, return an integer denoting the count of coins you collected.
Note:
You don’t need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10^2
0 <= N <= 6*10^2
|query.length| = 3

Time Limit: 1 sec
Sample Input 1:
2
6
+1A +3E -1A +4F +1A -3E
3
+0A +0B +0C
Sample Output 1:
4
3
Explanation For Sample Input 1:
Test Case 1: Please refer to the example above.

Test Case 2: Room 0A, 0B & 0C is booked so we collected 3 coins.
Sample Input 2:
2
4
+8D -8D +8D -8D
7
+3C +2B +7K -2B +6C -3C +5S
Sample Output 2:
2
5
Hint

The approach is simple just count the number of room booked queries.

Approaches (1)
Count Bookings

The approach is simple just count the number of room booked queries.

All we need to do is just traverse over each query string and check if the first character is a ‘+’. If it is a ‘+’ just increase the number of coins collected as the list describes a correct sequence of bookings, so each booking is valid.

 

The algorithm for the same is as follows:

 

  • Initialize count = 0.
  • Loop for each query:
    • If( query[0] == ‘+’ ):
      • count++
  • Return count.
Time Complexity

O(N) per test case where N is the number of queries.

 

In the worst case, we will be traversing over each query.

Space Complexity

O(1) per test case.

 

In the worst case, we are using constant extra space.

Code Solution
(100% EXP penalty)
Hotel Floors
Full screen
Console