leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0986.cpp (739B)


      1 class Solution {
      2   public:
      3     vector<vector<int>> intervalIntersection(vector<vector<int>> &firstList,
      4                                              vector<vector<int>> &secondList) {
      5         vector<vector<int>> res;
      6 
      7         int n = firstList.size(), m = secondList.size(), i = 0, j = 0;
      8         while (i < n && j < m) {
      9             const vector<int> &a = firstList[i], b = secondList[j];
     10             if (a[1] < b[0])
     11                 i++;
     12             else if (a[0] > b[1])
     13                 j++;
     14             else {
     15                 res.push_back({max(a[0], b[0]), min(a[1], b[1])});
     16                 if (a[1] < b[1])
     17                     i++;
     18                 else
     19                     j++;
     20             }
     21         }
     22 
     23         return res;
     24     }
     25 };