Make the intervals non-overlapping by assigning them to two different processors
Given a list of intervals interval[] where each interval contains two integers L and R, the task is to assign intervals to two different processors such that there are no overlapping intervals for each processor. To assign the interval[i] to the first processor, print “F” and to assign it to the second processor, print “S”.
Note: If there is no possible solution print -1.
Examples:
Input: interval[] = {{360, 480}, {420, 540}, {600, 660}}
Output: S, F, S
Explanation:
The intervals assigned to processors are –
Intervals of First Processor {{420, 540}}
Intervals of Second Processor {{360, 480}, {600, 660}}
As there are no overlapping intervals for each processor, it will be a valid solution.
Input: interval[] = {{99, 150}, {1, 100}, {100, 301}, {2, 5}, {150, 250}}
Output: S, F, F, S, S
Explanation:
The intervals assigned to processors are –
Intervals of First Processor {{1, 100}, {100, 301}}
Intervals of Second Processor {{99, 150}, {2, 5}, {150, 250}}
As there are no overlapping intervals for each processor, it will be a valid solution.
Approach: The idea is to use Greedy algorithm to assign the intervals to the processor.
If the highest end time of the processor is less than or equal to start time of an interval, then this interval can be assigned to the processor. Otherwise, check for the another processor. If any interval cannot be assigned to any processor then there is no possible solution.
Below is the illustration of the steps of the approach:
- As in the current problem we have to print according to the order of the intervals. So to save the order of intervals, pair the intervals with their index.
- Sort the intervals by their start time. i.e. L.
- Iterate over the intervals and assign the intervals to the processors as follows:
if (interval[i][0] >= firstProcessorEndTime)
answer[interval[i]] = "F"
firstProcessorEndTime =
max(firstProcessorEndTime, interval[i][0])
else if (interval[i][0] >= secondProcessorEndTime)
answer[interval[i]] = "S"
secondProcessorEndTime =
max(secondProcessorEndTime, interval[i][0])
else
print(-1)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void assignIntervals(vector<vector< int > > interval, int n)
{
for ( int i = 0; i < n; i++)
interval[i].push_back(i);
sort(interval.begin(), interval.end());
int firstEndTime = -1;
int secondEndTime = -1;
char find = ' ' ;
bool flag = false ;
for ( int i = 0; i < n; i++) {
if (interval[i][0] >= firstEndTime) {
firstEndTime = interval[i][1];
interval[i].push_back( 'S' );
}
else if (interval[i][0] >= secondEndTime) {
secondEndTime = interval[i][1];
interval[i].push_back( 'F' );
}
else {
flag = true ;
break ;
}
}
if (flag)
cout << (-1);
else {
vector< char > form(n, ' ' );
for ( int i = 0; i < n; i++) {
int indi = interval[i][2];
form[indi] = interval[i][3];
}
for ( int i = 0; i < form.size(); i++)
cout << form[i] << "," ;
}
}
int main()
{
vector<vector< int > > intervals
= { { 360, 480 }, { 420, 540 }, { 600, 660 } };
assignIntervals(intervals, intervals.size());
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
int [][] intervals = {
{ 360 , 480 },
{ 420 , 540 },
{ 600 , 660 }
};
assignIntervals(intervals, intervals.length);
}
public static void assignIntervals( int [][] interval, int n) {
for ( int i = 0 ; i < n; i++) {
interval[i] = Arrays.copyOf(interval[i], interval[i].length + 1 );
interval[i][interval[i].length - 1 ] = i;
}
Arrays.sort(interval, Comparator.comparingInt(a -> a[ 0 ]));
int firstEndTime = - 1 ;
int secondEndTime = - 1 ;
boolean flag = false ;
for ( int i = 0 ; i < n; i++) {
if (interval[i][ 0 ] >= firstEndTime) {
firstEndTime = interval[i][ 1 ];
interval[i] = Arrays.copyOf(interval[i], interval[i].length + 1 );
interval[i][interval[i].length - 1 ] = 'S' ;
} else if (interval[i][ 0 ] >= secondEndTime) {
secondEndTime = interval[i][ 1 ];
interval[i] = Arrays.copyOf(interval[i], interval[i].length + 1 );
interval[i][interval[i].length - 1 ] = 'F' ;
} else {
flag = true ;
break ;
}
}
if (flag) {
System.out.println( "-1" );
} else {
List<Character> form = new ArrayList<>(Collections.nCopies(n, ' ' ));
for ( int i = 0 ; i < n; i++) {
int indi = interval[i][ 2 ];
form.set(indi, ( char ) interval[i][ 3 ]);
}
String output = form.toString();
System.out.println(output);
}
}
}
|
Python3
def assignIntervals(interval, n):
for i in range (n):
interval[i].append(i)
interval.sort(key = lambda x: x[ 0 ])
firstEndTime = - 1
secondEndTime = - 1
find = ''
flag = False
for i in range (n):
if interval[i][ 0 ] > = firstEndTime:
firstEndTime = interval[i][ 1 ]
interval[i].append( 'S' )
elif interval[i][ 0 ] > = secondEndTime:
secondEndTime = interval[i][ 1 ]
interval[i].append( 'F' )
else :
flag = True
break
if flag:
print ( - 1 )
else :
form = [''] * n
for i in range (n):
indi = interval[i][ 2 ]
form[indi] = interval[i][ 3 ]
print (form, ", " )
if __name__ = = "__main__" :
intervals = [[ 360 , 480 ], [ 420 , 540 ], [ 600 , 660 ]]
assignIntervals(intervals, len (intervals))
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class MainClass {
public static void Main( string [] args) {
int [][] intervals = {
new int [] {360, 480},
new int [] {420, 540},
new int [] {600, 660}
};
AssignIntervals(intervals, intervals.Length);
}
public static void AssignIntervals( int [][] interval, int n) {
for ( int i = 0; i < n; i++) {
interval[i] = interval[i].Concat( new int [] {i}).ToArray();
}
interval = interval.OrderBy(x => x[0]).ToArray();
int firstEndTime = -1;
int secondEndTime = -1;
bool flag = false ;
for ( int i = 0; i < n; i++) {
if (interval[i][0] >= firstEndTime) {
firstEndTime = interval[i][1];
interval[i] = interval[i].Concat( new int [] { 'S' }).ToArray();
} else if (interval[i][0] >= secondEndTime) {
secondEndTime = interval[i][1];
interval[i] = interval[i].Concat( new int [] { 'F' }).ToArray();
} else {
flag = true ;
break ;
}
}
if (flag) {
Console.WriteLine( "-1" );
} else {
List< char > form = new List< char >( new char [n]);
for ( int i = 0; i < n; i++) {
int indi = interval[i][2];
form[indi] = ( char )interval[i][3];
}
string output = string .Join( "," , form);
Console.WriteLine( "[" +output+ "]" );
}
}
}
|
Javascript
function assignIntervals(interval, n) {
for (let i = 0; i < n; i++) {
interval[i].push(i);
}
interval.sort((a, b) => a[0] - b[0]);
let firstEndTime = -1;
let secondEndTime = -1;
let find = '' ;
let flag = false ;
for (let i = 0; i < n; i++) {
if (interval[i][0] >= firstEndTime) {
firstEndTime = interval[i][1];
interval[i].push( 'S' );
} else if (interval[i][0] >= secondEndTime) {
secondEndTime = interval[i][1];
interval[i].push( 'F' );
} else {
flag = true ;
break ;
}
}
if (flag) {
console.log(-1);
} else {
let form = Array(n).fill( '' );
for (let i = 0; i < n; i++) {
let indi = interval[i][2];
form[indi] = interval[i][3];
}
console.log(form, ", " );
}
}
let intervals = [[360, 480], [420, 540], [600, 660]];
assignIntervals(intervals, intervals.length);
|
Time Complexity: O(NlogN)
Auxiliary Space: O(N)
Last Updated :
17 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...