Created
June 4, 2023 23:23
-
-
Save thmain/7b660ecd323363a69e8f4aaf36ce9db5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
import java.util.List; | |
class Lecture { | |
int startTime; | |
int endTime; | |
public Lecture(int startTime, int endTime) { | |
this.startTime = startTime; | |
this.endTime = endTime; | |
} | |
} | |
public class MinimumHalls { | |
public static int minHalls(List<Lecture> lectures) { | |
// Sort lectures based on start time | |
Collections.sort(lectures, Comparator.comparingInt(l -> l.startTime)); | |
// List to keep track of allocated halls | |
List<List<Lecture>> halls = new ArrayList<>(); | |
for (Lecture lecture : lectures) { | |
boolean allocated = false; | |
// Check each existing hall for availability | |
for (List<Lecture> hall : halls) { | |
if (lecture.startTime > hall.get(hall.size() - 1).endTime) { | |
// Assign lecture to existing hall | |
hall.add(lecture); | |
allocated = true; | |
break; | |
} | |
} | |
if (!allocated) { | |
// Create a new hall and assign lecture to it | |
List<Lecture> newHall = new ArrayList<>(); | |
newHall.add(lecture); | |
halls.add(newHall); | |
} | |
} | |
// Number of halls used | |
return halls.size(); | |
} | |
public static void main(String[] args) { | |
List<Lecture> lectures = new ArrayList<>(); | |
lectures.add(new Lecture(9, 10)); | |
lectures.add(new Lecture(10, 11)); | |
lectures.add(new Lecture(11, 12)); | |
lectures.add(new Lecture(9, 12)); | |
int minimumHalls = minHalls(lectures); | |
System.out.println("Minimum number of halls required: " + minimumHalls); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment