Elegant C++ solution


  • 1
    G
    class Solution {
    public:
    
        struct Point
        {
            int val;
            bool start;
            
            friend bool operator<(const Point &P1, const Point &P2)
            {
                return (P1.val < P2.val || (P1.val == P2.val && P1.start == false));
            }
        };
        
        
        
        int minMeetingRooms(vector<Interval>& intervals) 
        {
            vector<Point> Points;
            
            if(intervals.empty())
            {
                return (0);
            }
            
            for(auto i : intervals)
            {
                Point s,e;
                s.val = i.start;
                s.start = true;
                e.val = i.end;
                e.start = false;
                Points.push_back(s);
                Points.push_back(e);
            }
            
            sort(Points.begin(),Points.end());
            
            int Rooms = 0;
            int MinRooms = 0;
            
            for(auto p : Points)
            {
                if(p.start)
                {
                    Rooms++;
                    MinRooms = max(MinRooms,Rooms);
                }
                else
                {
                    Rooms--;
                }
            }
            
            return (MinRooms);
        }
    };

Log in to reply
 

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