Open In App

Job Scheduling with two jobs allowed at a time

Last Updated : 09 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

We are given N jobs, and their starting and ending times. We can do two jobs simultaneously at a particular moment. If one job ends at the same moment some other show starts then we can’t do them. We need to check if it is possible to complete all the jobs or not.
Examples: 

Input :  Start and End times of Jobs
         1 2 
         2 3
         4 5 
Output : Yes
By the time third job starts, both jobs
are finished. So we can schedule third
job.

Input : Start and End times of Jobs
        1 5
        2 4
        2 6
        1 7
Output : No
All 4 jobs needs to be scheduled at time
3 which is not possible.

We first sort the jobs according to their starting time. Then we start two jobs simultaneously and check if the starting time of third job and so on is greater than the ending time of and of the previous two jobs. 
The implementation the above idea is given below. 
 

C++




// CPP program to check if all jobs can be scheduled
// if two jobs are allowed at a time.
#include <bits/stdc++.h>
using namespace std;
 
bool checkJobs(int startin[], int endin[], int n)
{
    // making a pair of starting and ending time of job
    vector<pair<int, int> > a;
    for (int i = 0; i < n; i++)
        a.push_back(make_pair(startin[i], endin[i]));
 
    // sorting according to starting time of job
    sort(a.begin(), a.end());
 
    // starting first and second job simultaneously
    long long tv1 = a[0].second, tv2 = a[1].second;
 
    for (int i = 2; i < n; i++) {
         
        // Checking if starting time of next new job
        // is greater than ending time of currently
        // scheduled first job
        if (a[i].first >= tv1)
        {
            tv1 = tv2;
            tv2 = a[i].second;
        }
         
        // Checking if starting time of next new job
        // is greater than ending time of currently
        // scheduled second job
        else if (a[i].first >= tv2)
            tv2 = a[i].second;
 
        else
            return false;
    }
    return true;
}
 
// Driver code
int main()
{
    int startin[] = { 1, 2, 4 }; // starting time of jobs
    int endin[] = { 2, 3, 5 }; // ending times of jobs
    int n = sizeof(startin) / sizeof(startin[0]);
    cout << checkJobs(startin, endin, n);
    return 0;
}


Java




// Java program to check if all jobs can be scheduled
// if two jobs are allowed at a time.
 
import java.util.*;
 
// Generic Pair class definition
class Pair<T, U> {
 
  // Data members
  private T key;
  private U value;
 
  // Constructor
  public Pair(T key, U value)
  {
    this.key = key;
    this.value = value;
  }
 
  // Getters
  public T getKey() { return key; }
 
  public U getValue() { return value; }
}
 
class GFG {
 
  public static boolean checkJobs(int[] startin,
                                  int[] endin, int n)
  {
    // making a pair of starting and ending time of job
    List<Pair<Integer, Integer> > a = new ArrayList<>();
    for (int i = 0; i < n; i++)
      a.add(new Pair<Integer, Integer>(startin[i],
                                       endin[i]));
 
    // sorting according to starting time of job
    Collections.sort(
      a, new Comparator<Pair<Integer, Integer> >() {
        public int compare(Pair<Integer, Integer> a,
                           Pair<Integer, Integer> b)
        {
          return a.getKey().compareTo(b.getKey());
        }
      });
 
    // starting key and value job simultaneously
    long tv1 = a.get(0).getValue(),
    tv2 = a.get(1).getValue();
 
    for (int i = 2; i < n; i++) {
      // Checking if starting time of next new job
      // is greater than ending time of currently
      // scheduled key job
      if (a.get(i).getKey() >= tv1) {
        tv1 = tv2;
        tv2 = a.get(i).getValue();
      }
      // Checking if starting time of next new job
      // is greater than ending time of currently
      // scheduled value job
      else if (a.get(i).getKey() >= tv2)
        tv2 = a.get(i).getValue();
 
      else
        return false;
    }
    return true;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int[] startin
      = { 1, 2, 4 }; // starting time of jobs
    int[] endin = { 2, 3, 5 }; // ending times of jobs
    int n = startin.length;
    System.out.println(checkJobs(startin, endin, n));
  }
}


Python3





C#




using System;
 
class Program
{
    static bool CheckJobs(int[] startin, int[] endin, int n)
    {
        // making a pair of starting and ending time of job
        (int, int)[] a = new (int, int)[n];
        for (int i = 0; i < n; i++)
            a[i] = (startin[i], endin[i]);
 
        // sorting according to starting time of job
        Array.Sort(a);
 
        // starting first and second job simultaneously
        long tv1 = a[0].Item2, tv2 = a[1].Item2;
 
        for (int i = 2; i < n; i++)
        {
            // Checking if starting time of next new job
            // is greater than ending time of currently
            // scheduled first job
            if (a[i].Item1 >= tv1)
            {
                tv1 = tv2;
                tv2 = a[i].Item2;
            }
            // Checking if starting time of next new job
            // is greater than ending time of currently
            // scheduled second job
            else if (a[i].Item1 >= tv2)
            {
                tv2 = a[i].Item2;
            }
            else
            {
                return false;
            }
        }
        return true;
    }
 
    static void Main()
    {
        int[] startin = { 1, 2, 4 }; // starting time of jobs
        int[] endin = { 2, 3, 5 }; // ending times of jobs
        int n = startin.Length;
        Console.WriteLine(CheckJobs(startin, endin, n));
    }
}


Javascript




// JS code
function checkJobs(startin, endin, n) {
    let a = [];
    for (let i = 0; i < n; i++) {
        a.push([startin[i], endin[i]]);
    }
    a.sort((a, b) => a[0] - b[0]);
    let tv1 = a[0][1], tv2 = a[1][1];
    for (let i = 2; i < n; i++) {
        if (a[i][0] >= tv1) {
            tv1 = tv2;
            tv2 = a[i][1];
        }
        else if (a[i][0] >= tv2) {
            tv2 = a[i][1];
        }
        else {
            return false;
        }
    }
    return true;
}
 
let startin = [1, 2, 4];
let endin = [2, 3, 5];
let n = startin.length;
console.log(checkJobs(startin, endin, n));


Output

1

Time Complexity: O(n log n), where n is the number of jobs. The sorting operation on the job array takes O(n log n) time, and the for loop iterates over the array once, which takes O(n) time. Hence, the overall time complexity is O(n log n).

Auxiliary Space: O(1). The algorithm uses constant extra space for storing temporary variables.

An alternate solution is to find maximum number of jobs that needs to be scheduled at any time. If this count is more than 2, return false. Else return true.

 



Previous Article
Next Article

Similar Reads

Find Jobs involved in Weighted Job Scheduling
Given N jobs where every job is represented by following three elements of it.1. Start Time 2. Finish Time 3. Profit or Value AssociatedFind the subset of jobs associated with maximum profit such that no two jobs in the subset overlap. Examples: Input: Number of Jobs n = 4 Job Details {Start Time, Finish Time, Profit} Job 1: {1, 2, 50} Job 2: {3, 5
11 min read
Weighted Job Scheduling in O(n Log n) time
Given N jobs where every job is represented by following three elements of it. Start TimeFinish TimeProfit or Value Associated Find the maximum profit subset of jobs such that no two jobs in the subset overlap.Example: Input: Number of Jobs n = 4 Job Details {Start Time, Finish Time, Profit} Job 1: {1, 2, 50} Job 2: {3, 5, 20} Job 3: {6, 19, 100} J
11 min read
Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling
Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling is like Multilevel Queue(MLQ) Scheduling but in this process can move between the queues. And thus, much more efficient than multilevel queue scheduling. Characteristics of Multilevel Feedback Queue Scheduling: In a multilevel queue-scheduling algorithm, processes are permanently assigned t
5 min read
Weighted Job Scheduling
Given N jobs where every job is represented by following three elements of it. Start TimeFinish TimeProfit or Value Associated (&gt;= 0) Find the maximum profit subset of jobs such that no two jobs in the subset overlap. Example: Input: Number of Jobs n = 4 Job Details {Start Time, Finish Time, Profit} Job 1: {1, 2, 50} Job 2: {3, 5, 20} Job 3: {6,
15 min read
Weighted Job Scheduling | Set 2 (Using LIS)
Given N jobs where every job is represented by following three elements of it.1. Start Time 2. Finish Time 3. Profit or Value AssociatedFind the maximum profit subset of jobs such that no two jobs in the subset overlap. Examples: Input: Number of Jobs n = 4 Job Details {Start Time, Finish Time, Profit} Job 1: {1, 2, 50} Job 2: {3, 5, 20} Job 3: {6,
10 min read
Program for Shortest Job First (or SJF) CPU Scheduling | Set 1 (Non- preemptive)
The shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with the smallest execution time to execute next. SJN, also known as Shortest Job Next (SJN), can be preemptive or non-preemptive. Characteristics of SJF Scheduling: Shortest Job first has the advantage of having a minimum average waiting time
13 min read
Shortest Job First (or SJF) CPU Scheduling Non-preemptive algorithm using Segment Tree
Shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with the smallest execution time to execute next. SJN is a non-preemptive algorithm.   Shortest Job first has the advantage of having a minimum average waiting time among all scheduling algorithms.It is a Greedy Algorithm.It may cause starvation i
26 min read
Sum of count of persons still performing the job whenever a person finishes job
Given two integers P denoting the number of people and a positive integer K. Each person is assigned with the same job which takes exactly K hours to finish. A condition is given that all of them can start performing their jobs exactly in X hours of interval. Count the number of persons still performing the job whenever a prior person finishes his/
6 min read
Minimum time required to complete all tasks with alteration of their order allowed
Given a string S consisting of N characters (representing the tasks to perform) and a positive integer K, the task is to find the minimum time required to complete all the given tasks in any order, given that each task takes one unit of time and each task of the same type must be performed at an interval of K units. Examples: Input: S = "AAABBB", K
8 min read
Minimum time to reach from Node 1 to N if travel is allowed only when node is Green
Given an undirected connected graph of N nodes and M edges. Each node has a light but at a time it can be either green or red. Initially, all the node is of green color. After every T seconds, the color of light changes from green to red and vice-versa. It is possible to travel from node U to node V only if the color of node U is green. The time ta
10 min read
Article Tags :
Practice Tags :