20 ms C++ solution using one vector


  • 0
    N

    For the meetings that can be in one room, we only keep the latest one. For the meeting that cannot be added to the current rooms, we open a new room. Welcome any comments for improvement.

    int minMeetingRooms(vector<Interval>& intervals) {
            int n = intervals.size();
            if (n <= 1) {
                return n;
            }
            sort(intervals.begin(), intervals.end(), [](Interval& x, Interval& y){return x.start < y.start;});
            vector<Interval> sol;
            sol.push_back(intervals[0]);
            for (int i = 1; i < n; i++) {
                int j;
                for (j = 0; j < sol.size(); j++) {
                    if (intervals[i].start >= sol[j].end) {
                        sol[j] = intervals[i];
                        break;
                    }
                }
                if (j == sol.size()) {
                    sol.push_back(intervals[i]);
                }
            }
            return sol.size();
        }
    

Log in to reply
 

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