Minimum halls required for class scheduling | Java implementation of the approach ; Function to return the minimum number of halls required ; Array to store the number of lectures ongoing at time t ; For every lecture increment start point s decrement ( end point + 1 ) ; Perform prefix sum and update the ans to maximum ; Driver code
import java . util . * ; class GFG { static int MAX = 100001 ; static int minHalls ( int lectures [ ] [ ] , int n ) { int [ ] prefix_sum = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { prefix_sum [ lectures [ i ] [ 0 ] ] ++ ; prefix_sum [ lectures [ i ] [ 1 ] + 1 ] -- ; } int ans = prefix_sum [ 0 ] ; for ( int i = 1 ; i < MAX ; i ++ ) { prefix_sum [ i ] += prefix_sum [ i - 1 ] ; ans = Math . max ( ans , prefix_sum [ i ] ) ; } return ans ; } public static void main ( String [ ] args ) { int lectures [ ] [ ] = { { 0 , 5 } , { 1 , 2 } , { 1 , 10 } } ; int n = lectures . length ; System . out . println ( minHalls ( lectures , n ) ) ; } }