Minimum days to perform all jobs in the given order
Last Updated :
12 Jan, 2023
Given a positive integer array jobs[] and positive integer K, jobs[i] represent the type of the ith job and K represents the minimum number of days that must pass before another job of the same type can be performed. The task is to find the minimum number of days needed to complete all jobs in the same order as the given job.
Note: Each day you can either take a break (don’t perform any job) or Complete a job.
Example:
Input: jobs[] = {1, 2, 1, 2, 3, 1}, K = 3
Output: 9
Explanation: One way to complete all jobs in 9 days is as follows:
Day 1: Complete the 0th job.
Day 2: Complete the 1st job.
Day 3: Take a break.
Day 4: Take a break.
Day 5: Complete the 2nd job.
Day 6: Complete the 3rd job.
Day 7: Take a break.
Day 8: Complete the 4th job.
Day 9: Complete the 5th job.
It can be shown that the jobs cannot be completed in less than 9 days.
Input: jobs[] = {5, 8, 8, 5}, K = 2
Output: 6
An approach using Hashing.
We’ll use map to keep track of minimum days that must pass to perform the ith type of job again.
Follow the steps below to implement the above idea:
- Initialize a map for mapping the job[i] with the minimum number of days that must pass to perform this job[i] again.
- Initialize a variable currDay = 0, this will keep track of the current day’s count.
- Iterate over the jobs[]
- Check if currDay is smaller than or equal to the days required to perform the ith job
- Then we’ll have to wait for (days required to perform the ith job – currDay) and additionally require one more day to perform the ith job. So, overall our currDay will become currDay += (unmap[days[i]] – currDay) + 1
- Otherwise, perform the ith job and increment the currDay by 1
- Update the map for an ith job type that can again perform after currDay + k days
- Return the value of currDays.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
long long minimiseJobTime(vector< int >& jobs, int K)
{
unordered_map< long long , long long > unmap;
long long currDay = 0;
for ( int i = 0; i < jobs.size(); i++) {
if (unmap[jobs[i]] >= currDay) {
currDay += (unmap[jobs[i]] - currDay) + 1;
}
else {
currDay += 1;
}
unmap[jobs[i]] = currDay + K;
}
return currDay;
}
int main()
{
vector< int > job = { 5, 8, 8, 5 };
int K = 2;
cout << minimiseJobTime(job, K);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int minimiseJobTime( int [] jobs, int K)
{
Map<Integer, Integer> unmap = new HashMap<>();
for ( int i = 0 ; i < jobs.length; i++)
{
unmap.put(jobs[i], 0 );
}
int currDay = 0 ;
for ( int i = 0 ; i < jobs.length; i++) {
if (unmap.get(jobs[i]) >= currDay) {
currDay += (unmap.get(jobs[i]) - currDay) + 1 ;
}
else {
currDay += 1 ;
}
unmap.put(jobs[i], currDay + K);
}
return currDay;
}
public static void main(String[] args) {
int [] job = new int [] { 5 , 8 , 8 , 5 };
int K = 2 ;
System.out.println(minimiseJobTime(job, K));
}
}
|
Python3
def minimiseJobTime(jobs, K) :
unmap = dict .fromkeys(jobs, 0 );
currDay = 0 ;
for i in range ( len (jobs)) :
if (unmap[jobs[i]] > = currDay) :
currDay + = (unmap[jobs[i]] - currDay) + 1 ;
else :
currDay + = 1 ;
unmap[jobs[i]] = currDay + K;
return currDay;
if __name__ = = "__main__" :
job = [ 5 , 8 , 8 , 5 ];
K = 2 ;
print (minimiseJobTime(job, K));
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static int minimiseJobTime( int [] jobs, int K)
{
Dictionary< int , int > unmap = new Dictionary< int , int >();
for ( int i = 0; i < jobs.Length; i++)
{
unmap[jobs[i]]=0;
}
int currDay = 0;
for ( int i = 0; i < jobs.Length; i++) {
if (unmap[jobs[i]] >= currDay) {
currDay += (unmap[jobs[i]] - currDay) + 1;
}
else {
currDay += 1;
}
unmap[jobs[i]] = currDay + K;
}
return currDay;
}
static public void Main (){
int [] job = new int [] { 5, 8, 8, 5 };
int K = 2;
Console.WriteLine(minimiseJobTime(job, K));
}
}
|
Javascript
function minimiseJobTime(jobs,K)
{
let unmap={};
let n= jobs.length;
for (let i=0;i<n+1;i++){
unmap[i]=0;
}
let currDay = 0;
for (let i = 0; i < n; i++) {
if (unmap[jobs[i]] >= currDay) {
currDay += (unmap[jobs[i]] - currDay) + 1;
}
else {
currDay += 1;
}
unmap[jobs[i]] = currDay + K;
}
return currDay;
}
let job = [ 5, 8, 8, 5 ];
let K = 2;
console.log(minimiseJobTime(job, K));
|
Time Complexity: O(N) where N is the size of the array
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...