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 their 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:

## Python3

`# Python implementation for intervals ` `# scheduling to two processors such ` `# that there are no overlapping intervals ` ` ` `# Function to assign the intervals ` `# to two different processors ` `def` `assignIntervals(interval, n): ` ` ` ` ` `# Loop to pair the interval ` ` ` `# with their indices ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `interval[i].append(i) ` ` ` ` ` `# sorting the interval by ` ` ` `# their startb times ` ` ` `interval.sort(key ` `=` `lambda` `x: x[` `0` `]) ` ` ` ` ` `firstEndTime ` `=` `-` `1` ` ` `secondEndTime ` `=` `-` `1` ` ` `fin ` `=` `'' ` ` ` `flag ` `=` `False` ` ` ` ` `# Loop to iterate over the ` ` ` `# intervals with their start time ` ` ` `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` ` ` ` ` `# Condition to check if there ` ` ` `# is a possible solution ` ` ` `if` `flag: ` ` ` `print` `(` `-` `1` `) ` ` ` `else` `: ` ` ` `form ` `=` `['']` `*` `n ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `indi ` `=` `interval[i][` `2` `] ` ` ` `form[indi] ` `=` `interval[i][` `3` `] ` ` ` `# form = ''.join(form) ` ` ` `print` `(form, ` `", "` `) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `intervals ` `=` `[[` `360` `, ` `480` `], [` `420` `, ` `540` `], [` `600` `, ` `660` `]] ` ` ` ` ` `# Function Call ` ` ` `assignIntervals(intervals, ` `len` `(intervals)) ` |

*chevron_right*

*filter_none*

**Output:**

['S', 'F', 'S'] ,

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find intersection of intervals given by two lists
- Find Intersection of all Intervals
- Maximal Disjoint Intervals
- Check if given intervals can be made non-overlapping by adding/subtracting some X
- Find least non-overlapping number from a given set of intervals
- Generate two BSTs from the given array such that maximum height among them is minimum
- Find two numbers with sum N such that neither of them contains digit K
- Split N into two integers whose addition to A and B makes them equal
- Check if two items can be selected from two different categories without exceeding price
- Rearrange an array to make similar indexed elements different from that of another array
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Sort an array of strings based on the frequency of good words in them
- Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them
- Minimum number of integers required such that each Segment contains at least one of them
- Minimum cost to reach a point N from 0 with two different operations allowed
- Find the minimum absolute difference in two different BST's
- Find two equal subsequences of maximum length with at least one different index
- Find pairs of elements from two different arrays whose product is a perfect square
- Count of common subarrays in two different permutations of 1 to N
- Minimum operations to make two numbers equal

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.