Minimal rely of subintervals to cowl a given time period

0
9
Adv1


Adv2

Given an array of intervals[], the duty is to search out the minimal rely of subintervals wanted to cowl a given time period. Every interval represents a phase of time that begins at a selected cut-off date (begin time) and ends at one other particular cut-off date (finish time). The intervals might overlap and have various lengths. If it’s inconceivable to cowl the whole period, return -1.

Examples:

Enter: Intervals[] = [[0, 1], [6, 8], [0, 2], [5, 6], [0, 4], [0, 3], [6, 7], [1, 3], [4, 7], [1, 4], [2, 5], [2, 6], [3, 4], [4, 5], [5, 7], [6, 9]], timeDuration = 9
Output: 3
Rationalization: We are able to take subintervals [0, 4], [4, 7], and [6, 9]. There are additionally many subintervals however that is the utmost amongst them.

Enter: Intervals[] = [[0, 2], [4, 6], [8, 10], [1, 9], [1, 5], [5, 9]], timeDuration = 10
Output: 3
Rationalization: We take the subintervals [0, 2], [8, 10], [1, 9]; a complete of three subintervals.

Strategy: This may be solved with the next concept:

Utilizing Dynamic Programming, and sorting the intervals on the idea of the beginning time, we will get what number of intervals it’s going to cowl if began from a selected time as much as time period.

Under are the steps concerned within the implementation of the code:

  • First, we type the given intervals in non-decreasing order of their begin occasions.
  • We create a 1-D DP array dp with dimension time+1, the place time is the given time period.
  • We initialize all components of dp to INT_MAX – 1, besides dp[0] = 0, since we will cowl 0-time period with 0 intervals.
  • If the present time period falls in any interval, we replace dp[i] as min(dp[i], dp[interval[0]] + 1), the place interval[0] is the beginning time of the interval and dp[interval[0]] + 1 means we’re including the present interval to the variety of subintervals wanted to cowl time period i.
  • Lastly, we return dp[time] if dp[time] < INT_MAX – 1, since dp[time] will retailer the minimal variety of subintervals wanted to cowl the given time period. In any other case, we return -1, indicating that it’s not doable to cowl the given time period utilizing the given intervals.

Under is the implementation of the above concept:

C++

#embody <bits/stdc++.h>

utilizing namespace std;

  

int CountSubintervals(vector<vector<int> >& Intervals,

                      int time)

{

  

    

    

    type(Intervals.start(), Intervals.finish());

  

    

    vector<int> dp(time + 1, INT_MAX - 1);

  

    dp[0] = 0;

    for (int i = 1; i <= time; i++) {

        for (auto& interval : Intervals) {

  

            

            

            

            

            

            

            

            if (interval[0] <= i && i <= interval[1]) {

                dp[i] = min(dp[i], dp[interval[0]] + 1);

            }

        }

    }

  

    

    

    

    return dp[time] == INT_MAX - 1 ? -1 : dp[time];

}

  

int major()

{

    vector<vector<int> > Intervals

        = { { 0, 1 }, { 6, 8 }, { 0, 2 }, { 5, 6 }, { 0, 4 }, { 0, 3 }, { 6, 7 }, { 1, 3 }, { 4, 7 }, { 1, 4 }, { 2, 5 }, { 2, 6 }, { 3, 4 }, { 4, 5 }, { 5, 7 }, { 6, 9 } };

    int timeDuration = 9;

  

    

    int consequence = CountSubintervals(Intervals, timeDuration);

    cout << consequence << endl;

    return 0;

}

Time Complexity: O(T*N)
Auxiliary House: O(T)

Adv3