코테준비 - Merge Intervals

정상화·2023년 2월 26일

LeetCode

목록 보기
54/222

Merge Intervals

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>> &intervals) {

        std::sort(intervals.begin(), intervals.end(), [](vector<int>& a, vector<int>& b) {
            return a.at(0) < b.at(0);
        });

        int start = intervals.at(0).at(0);
        int end = intervals.at(0).at(1);
        vector<vector<int>> res;

        auto it = intervals.begin() + 1;
        while (it != intervals.end()) {
            if (end >= (*it).at(0)) {
                end = max(end,(*it).at(1));
            }
            else{
                res.push_back({start, end});
                start = (*it).at(0);
                end = (*it).at(1);
            }
            it++;
        }
        res.push_back({start, end});
        return res;
    }
};
profile
백엔드 희망

0개의 댓글