 GeeksforGeeks App
Open App Browser
Continue

# Minimum days to perform all jobs in the given order

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++

 `// C++ code to implement the approach` `#include ``using` `namespace` `std;` `// Function to find the minimum time``// to complete the jobs in given order``long` `long` `minimiseJobTime(vector<``int``>& jobs, ``int` `K)``{``    ``// Initialize a map for mapping the``    ``// job[i] with minimum number of days``    ``// that must pass to perform this``    ``// job[i] again.``    ``unordered_map<``long` `long``, ``long` `long``> unmap;` `    ``// Initialize a variable currDay = 0,``    ``// this will keep track of current``    ``// days count``    ``long` `long` `currDay = 0;` `    ``// Iterate over array jobs[]``    ``for` `(``int` `i = 0; i < jobs.size(); i++) {` `        ``// Check if the minimum days``        ``// required to perform the ith job``        ``// is smaller than or equals to``        ``// days required to perform ith job``        ``// then we'll have to wait for``        ``// (days required to perform ith job - currDay)``        ``// and additionally require one more``        ``// day to perform the ith job. So,``        ``// overall our currDay will become``        ``// currDay will become currDay +=``        ``// (unmap[days[i]] - currDay) + 1;``        ``if` `(unmap[jobs[i]] >= currDay) {``            ``currDay += (unmap[jobs[i]] - currDay) + 1;``        ``}` `        ``// Otherwise, perform the ith job``        ``// any increment the currDay by 1``        ``else` `{``            ``currDay += 1;``        ``}` `        ``// Update the ith job type can``        ``// again perform after``        ``// currDay + k days.``        ``unmap[jobs[i]] = currDay + K;``    ``}` `    ``// return the currDays.``    ``return` `currDay;``}` `// Driver code``int` `main()``{``    ``vector<``int``> job = { 5, 8, 8, 5 };``    ``int` `K = 2;` `    ``// Function Call``    ``cout << minimiseJobTime(job, K);` `    ``return` `0;``}`

## Java

 `// Java code to implement the approach` `// Function to find the minimum time``// to complete the jobs in given order``import` `java.util.*;` `public` `class` `GFG {` `  ``// Function to find the minimum time``  ``// to complete the jobs in given order``  ``static` `int` `minimiseJobTime(``int``[] jobs, ``int` `K)``  ``{` `    ``// Initialize a map for mapping the``    ``// job[i] with minimum number of days``    ``// that must pass to perform this``    ``// job[i] again.``    ``Map unmap = ``new` `HashMap<>();``    ``for``(``int` `i = ``0``; i < jobs.length; i++)``    ``{``      ``unmap.put(jobs[i], ``0``);``    ``}` `    ``// Initialize a variable currDay = 0,``    ``// this will keep track of current``    ``// days count``    ``int` `currDay = ``0``;` `    ``// Iterate over array jobs[]``    ``for` `(``int` `i = ``0``; i < jobs.length; i++) {` `      ``// Check if the minimum days``      ``// required to perform the ith job``      ``// is smaller than or equals to``      ``// days required to perform ith job``      ``// then we'll have to wait for``      ``// (days required to perform ith job - currDay)``      ``// and additionally require one more``      ``// day to perform the ith job. So,``      ``// overall our currDay will become``      ``// currDay will become currDay +=``      ``// (unmap[days[i]] - currDay) + 1;``      ``if` `(unmap.get(jobs[i]) >= currDay) {``        ``currDay += (unmap.get(jobs[i]) - currDay) + ``1``;``      ``}` `      ``// Otherwise, perform the ith job``      ``// any increment the currDay by 1``      ``else` `{``        ``currDay += ``1``;``      ``}` `      ``// Update the ith job type can``      ``// again perform after``      ``// currDay + k days.``      ``unmap.put(jobs[i], currDay + K);``    ``}` `    ``// return the currDays.``    ``return` `currDay;``  ``}` `  ``// Driver code``  ``public` `static` `void` `main(String[] args) {``    ``int``[] job = ``new` `int``[] { ``5``, ``8``, ``8``, ``5` `};``    ``int` `K = ``2``;` `    ``// Function Call``    ``System.out.println(minimiseJobTime(job, K));``  ``}``}` `// This code is contributed by Harshad`

## Python3

 `# Python3 code to implement the approach` `# Function to find the minimum time``# to complete the jobs in given order``def` `minimiseJobTime(jobs, K) :` `    ``# Initialize a map for mapping the``    ``# job[i] with minimum number of days``    ``# that must pass to perform this``    ``# job[i] again.``    ``unmap ``=` `dict``.fromkeys(jobs,``0``);` `    ``# Initialize a variable currDay = 0,``    ``# this will keep track of current``    ``# days count``    ``currDay ``=` `0``;` `    ``# Iterate over array jobs[]``    ``for` `i ``in` `range``(``len``(jobs)) :` `        ``# Check if the minimum days``        ``# required to perform the ith job``        ``# is smaller than or equals to``        ``# days required to perform ith job``        ``# then we'll have to wait for``        ``# (days required to perform ith job - currDay)``        ``# and additionally require one more``        ``# day to perform the ith job. So,``        ``# overall our currDay will become``        ``# currDay will become currDay +=``        ``# (unmap[days[i]] - currDay) + 1;``        ``if` `(unmap[jobs[i]] >``=` `currDay) :``            ``currDay ``+``=` `(unmap[jobs[i]] ``-` `currDay) ``+` `1``;` `        ``# Otherwise, perform the ith job``        ``# any increment the currDay by 1``        ``else` `:``            ``currDay ``+``=` `1``;``    ` `        ``# Update the ith job type can``        ``# again perform after``        ``# currDay + k days.``        ``unmap[jobs[i]] ``=` `currDay ``+` `K;` `    ``# return the currDays.``    ``return` `currDay;` `# Driver code``if` `__name__ ``=``=` `"__main__"` `:` `    ``job ``=` `[ ``5``, ``8``, ``8``, ``5` `];``    ``K ``=` `2``;` `    ``# Function Call``    ``print``(minimiseJobTime(job, K));` `    ``# This code is contributed by AnkThon`

## C#

 `// C# code to implement the approach``using` `System;``using` `System.Collections.Generic;` `public` `class` `GFG``{` `  ``// Function to find the minimum time``  ``// to complete the jobs in given order``  ``static` `int` `minimiseJobTime(``int``[] jobs, ``int` `K)``  ``{``    ` `    ``// Initialize a map for mapping the``    ``// job[i] with minimum number of days``    ``// that must pass to perform this``    ``// job[i] again.``    ``Dictionary<``int``, ``int``> unmap = ``new` `Dictionary<``int``, ``int``>();``    ``for``(``int` `i = 0; i < jobs.Length; i++)``    ``{``      ``unmap[jobs[i]]=0;``    ``}` `    ``// Initialize a variable currDay = 0,``    ``// this will keep track of current``    ``// days count``    ``int` `currDay = 0;` `    ``// Iterate over array jobs[]``    ``for` `(``int` `i = 0; i < jobs.Length; i++) {` `      ``// Check if the minimum days``      ``// required to perform the ith job``      ``// is smaller than or equals to``      ``// days required to perform ith job``      ``// then we'll have to wait for``      ``// (days required to perform ith job - currDay)``      ``// and additionally require one more``      ``// day to perform the ith job. So,``      ``// overall our currDay will become``      ``// currDay will become currDay +=``      ``// (unmap[days[i]] - currDay) + 1;``      ``if` `(unmap[jobs[i]] >= currDay) {``        ``currDay += (unmap[jobs[i]] - currDay) + 1;``      ``}` `      ``// Otherwise, perform the ith job``      ``// any increment the currDay by 1``      ``else` `{``        ``currDay += 1;``      ``}` `      ``// Update the ith job type can``      ``// again perform after``      ``// currDay + k days.``      ``unmap[jobs[i]] = currDay + K;``    ``}` `    ``// return the currDays.``    ``return` `currDay;``  ``}` `  ``// Driver code``  ``static` `public` `void` `Main (){``    ``int``[] job = ``new` `int``[] { 5, 8, 8, 5 };``    ``int` `K = 2;` `    ``// Function Call``    ``Console.WriteLine(minimiseJobTime(job, K));``  ``}``}` `// This code is contributed by Pushpesh Raj.`

## Javascript

 `// JS implementation``// Function to find the minimum time``// to complete the jobs in given order``function` `minimiseJobTime(jobs,K)``{``    ``// Initialize a map for mapping the``    ``// job[i] with minimum number of days``    ``// that must pass to perform this``    ``// job[i] again.``    ``let unmap={};``    ``let n= jobs.length;``    ``for``(let i=0;i= currDay) {``            ``currDay += (unmap[jobs[i]] - currDay) + 1;``        ``}` `        ``// Otherwise, perform the ith job``        ``// any increment the currDay by 1``        ``else` `{``            ``currDay += 1;``        ``}` `        ``// Update the ith job type can``        ``// again perform after``        ``// currDay + k days.``        ``unmap[jobs[i]] = currDay + K;``    ``}``    ``// return the currDays.``    ``return` `currDay;``}` `// driver code``    ``let job = [ 5, 8, 8, 5 ];``    ``let K = 2;` `    ``// Function Call``    ``console.log(minimiseJobTime(job, K));``// This code is contributed by ksam24000`

Output

`6`

Time Complexity: O(N) where N is the size of the array
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up