Program to Find Out the Best Interval to Remove in C++


Suppose we have a list of intervals (inclusive) that are potentially overlapping. Now consider there is an operation where we delete one interval, then merge the remaining intervals and then count the number of intervals left over. We have to find the maximum number of leftover intervals possible after removal.

So, if the input is like intervals = [ [5, 8], [6, 7], [7, 10], [9, 11]], then the output will be 2. This is because −

  • If we delete the interval [5, 8] we get [6, 11] as the merge.

  • If we delete the interval [6, 7] we get [5, 11] as the merge.

  • If we delete the interval [7, 10] we get [5, 8], [9, 11] as the merge.

  • If we delete the interval [9, 11] we get [5, 10] as the merge.

So removing [7, 10] is perfect.

To solve this, we will follow these steps −

  • Define an array memo of number pairs,

  • Define a function countIntervals(), this will take one 2D array intervals, i, end.

    • if i is same as size of intervals, then −

      • return 0

    • if memo[i].first_element < end, then −

      • return −infinity.

    • if memo[i].first_element is same as end, then −

      • return second element of memo[i]

    • if end < intervals[i, 0], then −

      • memo[i] := minimum of end and first of memo[i], 1 + countIntervals(intervals, i + 1, intervals[i, 1])

      • return second element of memo[i]

    • memo[i] := minimum of end and first element of memo[i] and countIntervals(intervals, i + 1, maximum of intervals[I, 1] and end)

    • return second value of memo[i]

  • From the main method do the following −

    • resize array memo to the size of intervals

    • sort the array intervals

    • count := 0, result := 0, end := − 1

    • Define an array temp

    • for i := 0 to i < size of intervals, update (increase i by 1), do −

      • result := maximum of result and count + countIntervals(intervals, i + 1, end)

      • if end < intervals[i, 0], then −

        • (increase count by 1)

      • end := maximum of end and intervals[i, 1]

    • return result

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> memo;
int countIntervals(vector<vector<int>>& intervals, int i, int end) {
   if (i == intervals.size()) return 0;
   if (memo[i].first < end)
   return INT_MIN;
   if (memo[i].first == end)
   return memo[i].second;
   if (end < intervals[i][0]) {
      memo[i] = {min(end, memo[i].first), 1 +
      countIntervals(intervals, i + 1, intervals[i][1])};
      return memo[i].second;
   }
   memo[i] = {min(end, memo[i].first),
   countIntervals(intervals, i + 1, max(intervals[i][1],
   end))};
   return memo[i].second;
}
int solve(vector<vector<int>>& intervals) {
   memo.clear();
   memo.resize(intervals.size(), {INT_MAX, −1});
   sort(intervals.begin(), intervals.end());
   int count = 0, result = 0, end = −1;
   vector<int> temp;
   for (int i = 0; i < intervals.size(); i++) {
      result = max(result, count + countIntervals(intervals, i + 1,
      end));
      if (end < intervals[i][0])
         count++;
      end = max(end, intervals[i][1]);
   }
   return result;
}
int main(){
   vector<vector<int>> v = {{5, 8}, {6, 7}, {7, 10}, {9, 11}};
   cout<<solve(v);
}

Input

{{5, 8}, {6, 7}, {7, 10}, {9, 11}}

Output

2

Updated on: 15-Dec-2020

130 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements