Sharing my 604ms C++ solution


  • 0
    T
    /**
     * Definition for an interval.
     * struct Interval {
     *     int start;
     *     int end;
     *     Interval() : start(0), end(0) {}
     *     Interval(int s, int e) : start(s), end(e) {}
     * };
     */
    
    bool compareInterval(Interval I1, Interval I2)
    {
        return (I1.start < I2.start);
    }
    
    class Solution {
    private:
        int minMeetingRoomsHelper(vector<Interval>& intervals, vector<bool>& visited)
        {
            int lastEnd = 0;
            int i, n = intervals.size();
            for(i=0; i<n; i++)
                if(visited[i]==false && intervals[i].start>=lastEnd)
                {
                    visited[i] = true;
                    lastEnd = intervals[i].end;
                }
            
            bool isNext = false;
            for(i=0; i<n; i++)
                if(visited[i]==false)
                {
                    isNext = true;
                    break;
                }
                
            if(isNext == false)
                return 1;
            else
                return 1 + minMeetingRoomsHelper(intervals, visited);
    }
    
    public:
        int minMeetingRooms(vector<Interval>& intervals) {
            int n = intervals.size();
            if(n==0)
                return 0;
            
            vector<bool> visited(n, false);
            sort(intervals.begin(), intervals.end(), compareInterval);
            
            return minMeetingRoomsHelper(intervals, visited);
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.