Share my c++ simple solution


  • 2
    O
    /**
     * Definition for an interval.
     * struct Interval {
     *     int start;
     *     int end;
     *     Interval() : start(0), end(0) {}
     *     Interval(int s, int e) : start(s), end(e) {}
     * };
     */
     
    class node {
    public:
        int val;
        int type;  // 0 for end, 1 for start
        node (int v, int t) : val(v), type(t) {}
        bool operator< (const node &other) const {
            if (val == other.val) {
                return type < other.type;
            }
            return val < other.val;
        }
    }; 
     
     
    class Solution {
    public:
        int minMeetingRooms(vector<Interval>& intervals) {
            vector<node> v;
            for (auto i : intervals) {
                v.push_back(node(i.start, 1));
                v.push_back(node(i.end, 0));
            }
            sort(v.begin(), v.end());
            int ans = 0;
            int tmp_ans = 0;
            for (auto i : v) {
                if (i.type == 1) {
                    tmp_ans++;
                } else {
                    tmp_ans--;
                }
                ans = max(ans, tmp_ans);
            }
            return ans;
        }
    };

Log in to reply
 

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