THINK FIRST·CODE LATER

← All labs

Booking one room: which greedy rule is right?

Problem

One meeting room, n meeting requests name start end. Accept as many meetings as possible; two meetings are compatible if one ends before or exactly when the other starts.

Write RoomBooking with a method schedule(meetings, comparator) that sorts by the comparator and greedily accepts every meeting compatible with the last accepted one. Try three greedy rules — earliest finish first, earliest start first, shortest first (ties by name) — and print each schedule.

For the input

6
Long 0 20
A 1 3
B 3 6
C 6 9
Short 2 4
D 9 12

the program prints

Earliest finish first: 4 meetings: A[1-3] B[3-6] C[6-9] D[9-12]
Earliest start first:  1 meetings: Long[0-20]
Shortest first:        4 meetings: A[1-3] B[3-6] C[6-9] D[9-12]
Earliest finish is never beaten: true

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

d8-3-data-1.txt

6
Long 0 20
A 1 3
B 3 6
C 6 9
Short 2 4
D 9 12
What your program must print for d8-3-data-1.txt
Earliest finish first: 4 meetings: A[1-3] B[3-6] C[6-9] D[9-12]
Earliest start first:  1 meetings: Long[0-20]
Shortest first:        4 meetings: A[1-3] B[3-6] C[6-9] D[9-12]
Earliest finish is never beaten: true

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < d8-3-data-1.txt

Windows PowerShell does not understand <:

Get-Content d8-3-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.