Advertisement
thewitchking

Untitled

Dec 14th, 2023
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. // Java implementation of the above approach
  2. import java.util.*;
  3.  
  4. class GFG {
  5.     // Function to return the minimum
  6.     // number of halls required
  7.     static int minHalls(int lectures[][], int n)
  8.     {
  9.  
  10.         // Initialize a vector of pair, Time, first value
  11.         // indicates the time of entry or exit of a lecture
  12.         // second value denotes whether the lecture starts
  13.         // or ends
  14.         ArrayList<pair> Time = new ArrayList<>();
  15.  
  16.         // Store the lecture times
  17.         for (int i = 0; i < n; i++) {
  18.             Time.add(new pair(lectures[i][0], 1));
  19.             Time.add(new pair(lectures[i][1], -1));
  20.         }
  21.  
  22.         // Sort the vector
  23.         Collections.sort(Time, (pair A, pair B) -> {
  24.             return A.first - B.first;
  25.         });
  26.  
  27.         int answer = 0, sum = 0;
  28.  
  29.         // Traverse the Time vector and Update sum and
  30.         // answer variables
  31.         for (int i = 0; i < Time.size(); i++) {
  32.             sum += Time.get(i).second;
  33.             answer = Math.max(answer, sum);
  34.         }
  35.  
  36.         // Return the answer
  37.         return answer;
  38.     }
  39.  
  40.     static class pair {
  41.         int first, second;
  42.         pair(int x, int y)
  43.         {
  44.             first = x;
  45.             second = y;
  46.         }
  47.     }
  48.  
  49.     // Driver Code
  50.     public static void main(String[] args)
  51.     {
  52.         int lectures[][]
  53.             = { { 0, 5 }, { 1, 2 }, { 1, 10 } };
  54.         int n = lectures.length;
  55.  
  56.         System.out.println(minHalls(lectures, n));
  57.     }
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement