Hotel Room Booking

Problem

Rooms are booked for nights. A booking book ROOM FROM TO covers the nights FROM, FROM+1, ..., TO-1 (the night TO is free again). Print booked, or conflict if some night is already taken in that room. A command cancel ROOM FROM TO removes the booking that has exactly these numbers: print cancelled, or not found. Different rooms never conflict.

Constraints

  • 1 <= commands <= 100
  • 0 <= FROM < TO <= 999

Examples

Input

book 101 1 5
book 101 5 8
book 101 4 6

Output

booked
booked
conflict

Solutions

Best approachTime O(n * b)Space O(n)

Two half-open intervals [a, b) and [c, d) overlap exactly when a < d and c < b. The Room checks every old booking, so one booking costs O(bookings in the room).

Same code works in Java810172125Basic features only, so nothing changes between versions.
/*
 * Problem : Run the bookings.
 * Input   : Command lines.
 * Output  : Answer lines.
 * Approach: Room class with intervals
 * Time    : O(n * b)
 * Space   : O(n)
 * Java    : 8 or newer.
 */
import java.util.Scanner;

public class Main {
        // Step 1: A Room keeps its bookings as pairs [from, to). Two bookings overlap when each one starts before the other one ends.
        static class Room {
            private final java.util.List<int[]> bookings = new java.util.ArrayList<>();

            boolean book(int from, int to) {
                for (int[] other : bookings) {
                    if (from < other[1] && other[0] < to) {
                        return false;
                    }
                }
                bookings.add(new int[] {from, to});
                return true;
            }

            // Step 2: Cancel removes the booking with exactly the same numbers.
            boolean cancel(int from, int to) {
                for (int i = 0; i < bookings.size(); i++) {
                    if (bookings.get(i)[0] == from && bookings.get(i)[1] == to) {
                        bookings.remove(i);
                        return true;
                    }
                }
                return false;
            }
        }

    // This method solves the task. It receives the input and returns the answer.
    static String[] solve(String[] lines) {
        java.util.Map<String, Room> rooms = new java.util.HashMap<String, Room>();
        String[] out = new String[lines.length];
        for (int i = 0; i < lines.length; i++) {
            String[] part = lines[i].split(" ");
            Room room = rooms.computeIfAbsent(part[1], key -> new Room());
            int from = Integer.parseInt(part[2]);
            int to = Integer.parseInt(part[3]);
            if (part[0].equals("book")) {
                out[i] = room.book(from, to) ? "booked" : "conflict";
            } else {
                out[i] = room.cancel(from, to) ? "cancelled" : "not found";
            }
        }
        return out;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        java.util.List<String> all = new java.util.ArrayList<>();
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine().trim();
            if (!line.isEmpty()) {
                all.add(line);
            }
        }
        String[] lines = all.toArray(new String[0]);
        System.out.println(String.join("\n", solve(lines)));
    }
}
Hints (1)
  1. Why do 1-5 and 5-8 not conflict?