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:
#include <bits/stdc++.h>
using namespace std;
// Function to check if all jobs can be scheduled
// if two jobs are allowed at a time.
bool jobScheduling(vector<vector<int>> &jobs) {
int n = jobs.size();
// sort the jobs based on the start time
sort(jobs.begin(), jobs.end());
for(int i = 0; i < n; i++) {
// to store the highest end time
int end = jobs[i][1];
// to store the count of overlapping jobs
int cnt = 1;
while(i < n - 1 && jobs[i + 1][0] <= end) {
cnt++;
end = max(end, jobs[i + 1][1]);
i++;
}
// if the count of overlapping
// jobs is greater than 2, return false
if(cnt > 2)
return false;
}
return true;
}
int main() {
vector<vector<int>> jobs = { {1, 2}, {2, 3}, {4, 5} };
if(jobScheduling(jobs))
cout << "True";
else
cout << "False";
return 0;
}
// Function to check if all jobs can be scheduled
// if two jobs are allowed at a time.
import java.util.*;
class GfG {
// Function to check if all jobs can be scheduled
// if two jobs are allowed at a time.
static boolean jobScheduling(int[][] jobs) {
int n = jobs.length;
// sort the jobs based on the start time
Arrays.sort(jobs, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return Integer.compare(a[0], b[0]);
}
});
for (int i = 0; i < n; i++) {
// to store the highest end time
int end = jobs[i][1];
// to store the count of overlapping jobs
int cnt = 1;
while (i < n - 1 && jobs[i + 1][0] <= end) {
cnt++;
end = Math.max(end, jobs[i + 1][1]);
i++;
}
// if the count of overlapping
// jobs is greater than 2, return false
if (cnt > 2)
return false;
}
return true;
}
public static void main(String[] args) {
int[][] jobs = { {1, 2}, {2, 3}, {4, 5} };
if (jobScheduling(jobs))
System.out.println("True");
else
System.out.println("False");
}
}
# Function to check if all jobs can be scheduled
# if two jobs are allowed at a time.
def jobScheduling(jobs):
n = len(jobs)
# sort the jobs based on the start time
jobs.sort(key=lambda x: x[0])
i = 0
while i < n:
# to store the highest end time
end = jobs[i][1]
# to store the count of overlapping jobs
cnt = 1
while i < n - 1 and jobs[i + 1][0] <= end:
cnt += 1
end = max(end, jobs[i + 1][1])
i += 1
# if the count of overlapping
# jobs is greater than 2, return false
if cnt > 2:
return False
i += 1
return True
if __name__ == "__main__":
jobs = [ [1, 2], [2, 3], [4, 5] ]
if jobScheduling(jobs):
print("True", end="")
else:
print("False", end="")
// Function to check if all jobs can be scheduled
// if two jobs are allowed at a time
using System;
using System.Collections.Generic;
class GfG {
// Function to check if all jobs can be scheduled
// if two jobs are allowed at a time
static bool jobScheduling(List<List<int>> jobs) {
int n = jobs.Count;
// sort the jobs based on the start time
jobs.Sort((a, b) => a[0].CompareTo(b[0]));
for (int i = 0; i < n; i++) {
// to store the highest end time
int end = jobs[i][1];
// to store the count of overlapping jobs
int cnt = 1;
while (i < n - 1 && jobs[i + 1][0] <= end) {
cnt++;
end = Math.Max(end, jobs[i + 1][1]);
i++;
}
// if the count of overlapping
// jobs is greater than 2, return false
if (cnt > 2)
return false;
}
return true;
}
static void Main() {
List<List<int>> jobs = new List<List<int>> {
new List<int> {1, 2},
new List<int> {2, 3},
new List<int> {4, 5}
};
if (jobScheduling(jobs))
Console.WriteLine("True");
else
Console.WriteLine("False");
}
}
// Function to check if all jobs can be scheduled
// if two jobs are allowed at a time.
function jobScheduling(jobs) {
let n = jobs.length;
// sort the jobs based on the start time
jobs.sort((a, b) => a[0] - b[0]);
for (let i = 0; i < n; i++) {
// to store the highest end time
let end = jobs[i][1];
// to store the count of overlapping jobs
let cnt = 1;
while (i < n - 1 && jobs[i + 1][0] <= end) {
cnt++;
end = Math.max(end, jobs[i + 1][1]);
i++;
}
// if the count of overlapping
// jobs is greater than 2, return false
if (cnt > 2)
return false;
}
return true;
}
let jobs = [ [1, 2], [2, 3], [4, 5] ];
if (jobScheduling(jobs))
console.log("True");
else
console.log("False");
Output
True
Time Complexity: O(n * log n), to sort the given array jobs[].
Space Complexity: O(1)
Related Article: