Question

There are 10 floors in a hotel (numbered from 0 to 9). On each floor there...

There are 10 floors in a hotel (numbered from 0 to 9). On each floor there are 26 rooms, each marked with a capital letter of the English alphabet (from "A" to "Z").

Your task is to compute which room was booked most frequently, based on a list of room reservations. The list of reservations consists of N three-character strings. The first character of the string is "+" or "−", which describes whether the room was booked or freed. The second and third characters correspond to the number of the floor and letter of the room, respectively. For example "+4C" means that room C on the 4th floor has just been booked, and "−0G" means that room G on the 0th floor has been freed.

You may assume that the list describes a correct sequence of bookings in chronological order; that is, only free rooms can be booked and only booked rooms can be freed. All rooms are initially free. Note that this doesn't mean that all rooms have to be free at the end.

Write a function:

class Solution { public String solution(String[] A); }

that, given an array A consisting of N strings, representing the list of bookings, returns a two-character string consisting the floor number and letter of the room that was booked the most times. It is possible that more than one room might have been booked the same, maximum number of times; in this case, return the one whose identifier is the smallest alpha-numerically.


0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

import java.util.*;
class Main {
    static String Solution(String[] A) {

        // Create HashMap to store rooms and frequency of bookings
        HashMap<String, Integer> rooms = new HashMap<String, Integer>();
        int max = 0; // max to count highest frequenncy
      
        for (int i = 0; i < A.length; i++) {
            if ( rooms.containsKey(A[i]) ) {
                rooms.put(A[i], rooms.get(A[i]) + 1);
                int val = rooms.get(A[i]);
                max = max < val ? val :max;
            }
            else
                rooms.put(A[i], 1);
        }

        // Create set to iterate on HashMap
        Set<Map.Entry<String, Integer> > set = rooms.entrySet();
        String room="";
        int floor = 10; char room_no = 'Z';
      
        for (Map.Entry<String, Integer> r : set) {
            // Check for rooms having highest frequency
            if (r.getValue() == max) {
                room = r.getKey().substring(1,3); // removing the sign(+,-) using sub string.
                int f = Integer.parseInt(room.substring(0,room.length()-1));
                char rm = room.charAt(room.length()-1);
                //sort alpha-numerically.
                if(floor > f){
                    floor = f; room_no = rm;
                }
                else if(room_no > rm){
                    floor = f; room_no = rm;
                }
            }
        }
        room = String. valueOf(floor) + room_no;
        return room;
    }

    public static void main(String[] args) {
        String A[] = {"+4C", "+3D", "-4C", "+9F", "-3D", "+4C",
                        "+9E","+4F", "-4F","+3D","+4F"};
        String sol = Solution(A);
        System.out.println(sol);
    }
}

Screenshot:

Hope you like the solution.

Please like it and give your valuable feedback or any doubt.

Add a comment
Know the answer?
Add Answer to:
There are 10 floors in a hotel (numbered from 0 to 9). On each floor there...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Let’s build a dynamic string tokenizer! Start with the existing template and work on the areas...

    Let’s build a dynamic string tokenizer! Start with the existing template and work on the areas marked with TODO in the comments: Homework 8 Template.c Note: If you turn the template back into me without adding any original work you will receive a 0. By itself the template does nothing. You need to fill in the code to dynamically allocate an array of strings that are returned to the user. Remember: A string is an array. A tokenizer goes through...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT