VOOZH about

URL: https://www.geeksforgeeks.org/dsa/job-scheduling-two-jobs-allowed-time/

⇱ Job Scheduling with two jobs allowed at a time - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Job Scheduling with two jobs allowed at a time

Last Updated : 15 Mar, 2025

Given a 2d array jobs[][] of order n * 2, where each element jobs[i], contains two integers, representing the start and end time of the job. Your task is to check if it is possible to complete all the jobs, provided that two jobs can be done simultaneously at a particular moment.

Note: If a job start at the end of any other job, then both the jobs will be considered to be overlapping.

Example:

Input: jobs[][] = [ [1, 2], [2, 3], [4, 5] ]
Output: True
Explanation: The first and second job overlap at time segment [2, 2], and can be performed simultaneously, and both the jobs get completed before the start of third job.

Input: jobs[][] = [ [1, 5], [2, 4], [2, 6], [1, 7] ]
Output: False
Explanation: All four jobs overlap at time segment [2, 5], and can't be completed together.

Approach:

The idea is to sort the jobs based on ascending order of their start time, and iteratively find the overlapping jobs. If at any time segment, more than two jobs are overlapping, then print "False", otherwise print "True".

Follow the below given steps:

  • Firstly sort the given array jobs[] based on the start time.
  • Thereafter, start iterating from the first job, till the last one.
  • Create a counter end and cnt, to store the highest end time and the count of overlapping jobs.
  • At each iteration, run a while loop until the end time of the current job is greater than or equals to the start time of next job.
  • Also, increment count by 1, and update the end time to the maximum of current and next job.
  • If at any point, the value of cnt > 2, then break the loop and print "False".
  • Else after the loop ends, print "True"

Below is given the implementation:


Output
True

Time Complexity: O(n * log n), to sort the given array jobs[].
Space Complexity: O(1)

Related Article:

Comment
Article Tags:
Article Tags: