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:
def minRoundstoCompleteAllTasks(tasks, n):
tasks.sort(reverse = True) # Sorting tasks in descending order
totalTimeTaken = [0] * n # Creating an array to store time taken by each worker
for task in tasks:
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. def minimumRounds(tasks): # create a list to store the number of rounds # needed to complete all tasks minRounds = [0]*(len(tasks)+1) # iterate through all the tasks for i in range(len(tasks)): # if task i is not task 0 if (tasks[i] != 0): # initialize j to i j = i # create a variable to store the number # of tasks that need to be completed numTasks = 0 # 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 while (j < len(tasks)): # if the current task is not task 0 if (tasks[j] != 0): # increment the number of tasks numTasks += 1 # if the number of tasks is equal to the # number of rounds, break the loop if (numTasks == numRounds): 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 if (numTasks != numRounds): numRounds = float('inf') # update the value of minRounds at index i+1 minRounds[i+1] = min(minRounds[i], numRounds) # if task i is task 0 else: # update the value of minRounds at index i+1 minRounds[i+1] = minRounds[i] # return the minimum number of rounds required # to complete all tasks return minRounds[len(tasks)]
var minimumRounds = function(arr, k) { // your code goes here 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] == 0 rounds[i] != rounds[i + 1] for 0 < i < n #includeusing 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 // task till the current task vector seen(n); // To store the number of rounds // starting from the starting task // till the current task vector rounds_taken(n); // To store the tasks visited till // the current task vector > tasks_seen(n); // Mark the starting task as visited visited[rounds[0]] = true; // Initialize the number of tasks // visited from the starting task // till the current task as 1 seen[rounds[0]] = 1; // Initialize the number of rounds // starting from the starting task // till the current task as 1 rounds_taken[rounds[0]] = 1; // Insert the starting task to tasks_seen tasks_seen[rounds[0]].push_back(rounds[0]); // 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 // tasks_seen[rounds[i]]. If it is // same, then it means that we have // completed a cycle. if (tasks_seen[rounds[i]] == seen) 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]]++; // Insert the current task to tasks_seen tasks_seen[rounds[i]].push_back(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 if (tasks == null || tasks.Length == 0) { return 0; } int rounds = 0; //sort the tasks array in ascending order Array.Sort(tasks); //find the median task time int median = tasks[tasks.Length / 2]; //calculate the minimum number of rounds needed to complete all tasks rounds = (median - 1) * n + tasks.Length; return rounds; } }