Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Minimum Segments Given an array consisting of a number of intervals, each interval is of type ( a [ i ] , b [ i

Minimum Segments
Given an array consisting of a number of intervals, each interval is of type (a[i], b[i]). Also provided is an integer k. You are to add exactly one segment (at least, at most) into the array such that the length of the segment is exactly k and the modified array can be separated into the minimum number of connected sets.
A set of segments is connected if every point in the segment (from the minimum a[i] to the maximum b[i] among all i in the set) is covered by some segment in the set.
Example:
The set [(1,2),(2,3),(1,5)] is connected while the set [(0,2),(3,4)] is not because point 2.5 is not covered by any segment.
Consider the array a =[(1,5),(2,4),(6,6),(7,14),(16,19)] and given k =2; Adding a segment (5,7) into the array, after adding this segment, separate the array into 2 connected sets:
[(1,5),(2,4),(5,7),(6,6),(7,14)]
[(16,19)]
However, if we add a segment (14,16) into the array, then we have to separate the array using 3 connected sets:
[(1,5),(2,4)]
[(6,6),(7,14),(14,16)]
[(16,19)]
So,2 connected sets is the minimum answer that we can achieve.
Function Description:
Complete the function minimumDivision in the editor below. minimumDivision has the following parameters:
an integer array a of first parameters of intervals
an integer array b of second parameters of intervals
A: an integer denoting the maximum range of the segment that can be added.
Returns:
an integer denoting the minimum number of sets needed to separate the array after adding the segment.
Constraints:
1<= n <=2*10^5
1<= a[i], b[i]<=10^9
1<= k <=10^9

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Databases

Authors: Jeff Mapua

1st Edition

1978502257, 978-1978502253

More Books

Students also viewed these Databases questions