-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassrooms.java
More file actions
113 lines (90 loc) · 3.09 KB
/
classrooms.java
File metadata and controls
113 lines (90 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* Nathan Hicks
* COP 3503
* RP3 Classrooms
* Given a list of events with start an end times and the number of classrooms
* Uses a greedy algorithm to calculate max amount of events that could be scheduled
*/
//Imports
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.TreeSet;
public class classrooms {
public static void main(String[] args) {
//Scan in the number of activities and classrooms
Scanner in = new Scanner(System.in);
int activities = in.nextInt();
int rooms = in.nextInt();
int count = 0; //Max number of activities to be scheduled
//Tree set to track the rooms and arraylist to track the activities
TreeSet<Room> roomTracker = new TreeSet<>();
ArrayList<Activity> activitiesTracker = new ArrayList<>();
//Scans in all the start and end times for each activity
for (int i = 0; i < activities; i++)
activitiesTracker.add(new Activity(in.nextInt(), in.nextInt()));
//Sorts activities from lowest end time to highest
Collections.sort(activitiesTracker);
//Loop through all activities
for (Activity a : activitiesTracker) {
Room lowest = null; //Lowest end time in the rooms
int size = roomTracker.size(); //Number of rooms in use
//Tries to find a room with an end time closest but lower than the activities start time
try {
lowest = roomTracker.floor(new Room(a.s, rooms));
}
//Sets to null if fails
catch (Exception e) {
lowest = null;
}
//If could not find an end time lower than start time, add to empty room if one is available
if (lowest == null) {
if (size < rooms) {
roomTracker.add(new Room(a.e +1, size)); // +1 is to make floor function work properly
count++;
}
//If lowest was found, add activity to that room
} else {
roomTracker.remove(lowest);
roomTracker.add(new Room(a.e +1, lowest.id));
count++;
}
}
//Print result and closed scanner
System.out.println(count);
in.close();
}
//Activity object that contains the start and end time
static class Activity implements Comparable<Activity>{
int s; //Start time
int e; //End time
//Constructor
public Activity (int start, int end) {
s = start;
e = end;
}
//Comparable method to sort from lowest to highest end time then lowest to highest start time
public int compareTo(Activity o) {
int res = e - o.e;
if (res == 0) return s - o.s;
return res;
}
}
//Room object with end time, and id to make TreeSet work for duplicates
static class Room implements Comparable<Room>{
int e; //End time of most recent activity
int id; //Id for room
//Constructor
public Room (int end, int id) {
e = end;
this.id = id;
}
//Comparable method that sorts by lowest to highest end time, then by lowest to highest id
//Almost forgot to sort by lowest to highest id which would've completely undermined the point of having no dupes
public int compareTo(Room o) {
// TODO Auto-generated method stub
int res = e - o.e;
if (res == 0) return id - o.id;
return res;
}
}
}