C++ solution using sort and two pointers


  • 0
    L
    class Solution {
    public:
        int minMeetingRooms(vector<Interval>& intervals) {
          if(intervals.size() == 0) return 0;
          vector<int> startTimes(intervals.size());
          vector<int> endTimes(intervals.size());
          for(int i = 0; i<intervals.size(); i++){
            startTimes[i] = intervals[i].start;
            endTimes[i] = intervals[i].end;
          }
          sort(startTimes.begin(),startTimes.end());
          sort(endTimes.begin(),endTimes.end());
          int i = 0, j = 0;
          int ret = 0;
          int meeting = 0;
          while(i<intervals.size() || j<intervals.size()){
            if(j>=intervals.size() || (i<intervals.size() && startTimes[i] < endTimes[j])){
              if(++meeting>ret) ret = meeting;
              i++;
              continue;
            }
            if(i>=intervals.size() || (j<intervals.size() && startTimes[i] >= endTimes[j])){
              meeting--;
              j++;
              continue;
            }
          }
          return ret;
        }
    };
    

Log in to reply
 

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