nikunjsoni

986

Apr 12th, 2021
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     vector<vector<int>> intervalIntersection(vector<vector<int>>& A, vector<vector<int>>& B) {
  4.         int a, b, a_start, a_end, b_start, b_end;
  5.         a = b = 0;
  6.         vector< vector<int> > res;
  7.         while(a < A.size() && b < B.size()){
  8.             a_start = A[a][0], a_end = A[a][1];
  9.             b_start = B[b][0], b_end = B[b][1];
  10.            
  11.             if(a_start <= b_end && b_start <= a_end){
  12.                 int left = max(a_start, b_start);
  13.                 int right = min(a_end, b_end);
  14.                 res.push_back(vector<int> {left, right});
  15.             }
  16.            
  17.             if(a_end <= b_end)
  18.                 a++;
  19.             else
  20.                 b++;
  21.         }
  22.         return res;
  23.     }
  24. };
Advertisement
Add Comment
Please, Sign In to add comment