[LeetCode] 986. Interval List Intersections - c++

ha·2022년 1월 14일
0

LeetCode

목록 보기
3/21

투포인터 풀이

List A와 B 각각 포인터를 두고 각각 시작과 끝을 비교

class Solution {
public:
    vector<vector<int>> intervalIntersection(vector<vector<int>>& firstList, vector<vector<int>>& secondList) {
        vector<vector<int>> ans;
        int i=0, j=0;
        while(i<firstList.size() && j<secondList.size()){
            int low = max(firstList[i][0], secondList[j][0]);
            int high = min(firstList[i][1], secondList[j][1]);
            if(low<=high){
                ans.push_back({low, high});
            }
            if(firstList[i][1] < secondList[j][1]){
                i++;
            } else {
                j++;
            }
        }
        return ans;
    }
};```

0개의 댓글