# Solution For Minimum Rounds To Complete All Tasks

Problem Statement:

Given an array tasks representing the tasks that need to be done, where tasks[i] is the time it takes to complete the ith task. Also, there are n workers, where each worker can only complete one task at a time.

Return the minimum number of rounds to complete all the tasks.

The algorithm must satisfy the following conditions:
1. Each worker can complete only one task at a time.
2. Each worker finishes his assigned task before taking a new one.
3. The assignment of tasks cannot be changed.

Solution:

To solve this problem, we can follow the following approach:
1. First, we sort the array of tasks in descending order since we want to assign the largest tasks to the workers first. This will help us to minimize the number of rounds required to complete all tasks.
2. We create an array of size n, where we store the time taken by each worker to complete their assigned task. Initially, all elements of the array will be set to 0 as no worker has been assigned any task yet.
3. We loop through all tasks and assign each task to the worker who has the minimum time taken so far. We update the time taken by that worker to the sum of his current time and the time required to complete the task.
4. Finally, the number of rounds required to complete all the tasks will be equal to the maximum time taken by any worker.

Here is the Python code implementing the above approach:

totalTimeTaken =  * n # Creating an array to store time taken by each worker
minTimeTaken = min(totalTimeTaken) # Finding the worker with minimum time taken so far
totalTimeTaken[totalTimeTaken.index(minTimeTaken)] += task # Updating time taken by the worker
return max(totalTimeTaken) # Returning the maximum time taken by any worker

Here, the time complexity of the solution is O(n log n), which is the time complexity of sorting the tasks array. The loop for assigning tasks will take O(N) time. Therefore, the overall time complexity of the solution is O(N log N).

Example:

tasks = [4, 3, 2, 6, 7, 2, 4] n = 3
Output: 12

In this example, we have 7 tasks and 3 workers. These are the tasks assigned to workers in each round:

Round 1: worker 1 takes task 7 (time taken = 7)
Round 2: worker 2 takes task 6 (time taken = 6)
Round 3: worker 3 takes task 4 (time taken = 4)
Round 4: worker 1 takes task 4 (time taken = 11)
Round 5: worker 2 takes task 3 (time taken = 9)
Round 6: worker 3 takes task 2 (time taken = 6)
Round 7: worker 1 takes task 2 (time taken = 13)

Therefore, the total number of rounds required to complete all tasks is 7.

## Step by Step Implementation For Minimum Rounds To Complete All Tasks

```class Solution {
public int minimumRounds(int[] tasks, int n) {
// TODO: Write your code here
return 0;
}
}```
```There are n tasks, labeled from 0 to n-1.
Each task can be done in one of two ways:

1. Task 0 is done first, then all other tasks are done in the same order as they appear in the array.
2. All other tasks are done in the same order as they appear in the array, then task 0 is done.
Return the minimum number of rounds that must be completed to finish all the tasks.

# create a list to store the number of rounds
# needed to complete all tasks

# iterate through all the tasks

# initialize j to i
j = i

# create a variable to store the number
# of tasks that need to be completed

# create a variable to store the number
# of rounds required to complete the tasks
numRounds = 1

# iterate through all the tasks from i to n-1

# increment the number of tasks

# if the number of tasks is equal to the
# number of rounds, break the loop
break

# increment j
j += 1

# if the number of tasks is not equal to the
# number of rounds, set the number of rounds
# required to complete the tasks to infinity
numRounds = float('inf')

# update the value of minRounds at index i+1
minRounds[i+1] = min(minRounds[i], numRounds)

else:

# update the value of minRounds at index i+1
minRounds[i+1] = minRounds[i]

# return the minimum number of rounds required
```var minimumRounds = function(arr, k) {
let n = arr.length;

// if k is greater than or equal to n,
// then there is no need to do anything
if (k >= n)
return 0;

// Sort the given array in reverse order
arr.sort((a, b) => b - a);

// Find sum of first k elements
let sum = 0;
for (let i = 0; i < k; i++)
sum += arr[i];

// Initialize result
let res = sum;

// Do for remaining array elements
for (let i = k; i < n; i++)
{
// Add current element arr[i] to result
res += arr[i];

// Find sum of first k elements
// of updated array
sum += arr[i] - arr[i-k];

// Update result if required
res = Math.min(res, sum);
}

return res;
}```
```There are n tasks, labeled from 0 to n - 1. Tasks may have some prerequisite tasks, for example to task 0 you need to complete tasks 1, which is expressed as a pair: [0, 1]

Given the number of rounds m and the integer array rounds where rounds[i] denotes the ith round's tasks order, return the minimum number of rounds to complete all tasks.

Constraints:

1 <= n <= 10^5
1 <= m <= 10^5
rounds.length == n + 1
0 <= rounds[i] < n
rounds == 0
rounds[i] != rounds[i + 1] for 0 < i < n

#include
using namespace std;

// Function to return the minimum
// number of rounds
int minRounds(int n, int m,
vector& rounds)
{

// To store the visited tasks
vector visited(n);

// To store the number of tasks
// visited from the starting
vector seen(n);

// To store the number of rounds
// starting from the starting task
vector rounds_taken(n);

// To store the tasks visited till

// Mark the starting task as visited
visited[rounds] = true;

// Initialize the number of tasks
// visited from the starting task
// till the current task as 1
seen[rounds] = 1;

// Initialize the number of rounds
// starting from the starting task
// till the current task as 1
rounds_taken[rounds] = 1;

// Start from the second task
for (int i = 1; i <= m; i++) {

// If the current task is visited
// before, then it means that we
// have completed a cycle
if (visited[rounds[i]]) {

// Check if the tasks seen till
// the current task is same as
// same, then it means that we have
// completed a cycle.
return rounds_taken[rounds[i]];
}

// Mark the current task as visited
visited[rounds[i]] = true;

// Increment the number of tasks
// visited from the starting task
// till the current task by 1
seen[rounds[i]]++;

// Increment the number of rounds
// starting from the starting task
// till the current task by 1
rounds_taken[rounds[i]] = rounds_taken[rounds[i - 1]] + 1;
}

// If we reach here, then it means
// that we have not completed any
// cycle and we have completed
// all the tasks, so return m
return m;
}```
```using System;
using System.Collections.Generic;
using System.Linq;

public class Solution {

public int MinimumRounds(int[] tasks, int n) {

//tasks is an array of integers representing the time it takes to complete each task
//n is the maximum number of tasks that can be completed in a single round

//return the minimum number of rounds required to complete all tasks

return 0;
}

int rounds = 0;

//sort the tasks array in ascending order

//calculate the minimum number of rounds needed to complete all tasks
rounds = (median - 1) * n + tasks.Length;

return rounds;
}
}```

Scroll to Top

## Top 100 Leetcode Practice Problems In Java

Get 30% Off Instantly!
[gravityforms id="5" description="false" titla="false" ajax="true"]