在 D 天内完成给定任务的每天最少工作量
给定一个大小为N的数组task[]表示每个任务要完成的工作量,问题是找到每天要完成的最小工作量,以便最多可以在D天内完成所有任务。
注意:一天只能完成一项任务。
例子:
Input: task[] = [3, 4, 7, 15], D = 10
Output: 4
Explanation: Here minimum work to be done is 4.
On 1st day, task[0] = 3 < 4 so this task can only be completed. Because work can be done for only one task on one day.
For task[1] = 4 the task can be completed in 1 day.
Task[2] = 7. Now 4 amount of work can be done in 1 day so to complete this task 2 days are required.
Task[3] = 15, 4 additional days are required.
Total number of days required = 1 + 1 + 2 + 4 = 8 days < D.
So 4 value would be the minimum value.
Input: task[] = [30, 20, 22, 4, 21], D = 6
Output: 22
方法:可以使用二进制搜索方法使用以下思想解决问题:
The minimum work that can be done each day is 1 and the maximum work that can be done is the maximum of the tasks array.
Search on this range and if the mid-value satisfies the condition then move to 1st half of the range else to the second half.
按照提到的步骤实施该方法:
- 创建一个变量left = 1 ,表示范围的起点,变量right = INT_MIN 。
- 运行从index = 0 到 index = N – 1的循环并更新right = max(right, task[index])。
- 创建一个变量per_day_task = 0来存储答案。
- 使用left <= right运行一段时间条件
- 创建一个变量mid = left + (right – left)/2。
- 如果所有任务都可以在D天内通过每天做中等量的工作来完成,则更新per_day_task = mid并使正确 = mid – 1。
- 否则正确 = 中 + 1。
- 打印per_day_task作为每天完成所有任务的最少任务数。
以下是上述方法的实现:
C++
// C++ code to implement the approach
#include
using namespace std;
// Function to check if
// all the task can be
// completed by 'per_day'
// number of task per day
bool valid(int per_day,
vector task, int d)
{
// Variable to store days required
// to done all tasks
int cur_day = 0;
for (int index = 0; index < task.size();
index++) {
int day_req
= ceil((double)(task[index])
/ (double)(per_day));
cur_day += day_req;
// If more days required
// than 'd' days so invalid
if (cur_day > d) {
return false;
}
}
// Valid if days are less
// than or equal to 'd'
return cur_day <= d;
}
// Function to find minimum
// task done each day
int minimumTask(vector task, int d)
{
int left = 1;
int right = INT_MAX;
for (int index = 0;
index < task.size();
index++) {
right = max(right, task[index]);
}
// Variable to store answer
int per_day_task = 0;
while (left <= right) {
int mid = left
+ (right - left) / 2;
// If 'mid' number of task per day
// is valid so store as answer and
// more to first half
if (valid(mid, task, d)) {
per_day_task = mid;
right = mid - 1;
}
else {
left = mid + 1;
}
}
// Print answer
return per_day_task;
}
// Driver Code
int main()
{
// Input taken
vector task{ 3, 4, 7, 15 };
int D = 10;
cout << minimumTask(task, D) << endl;
return 0;
}
Java
// JAVA code to implement the approach
import java.util.*;
class GFG
{
// Function to check if
// all the task can be
// completed by 'per_day'
// number of task per day
public static boolean
valid(int per_day, ArrayList task, int d)
{
// Variable to store days required
// to done all tasks
int cur_day = 0;
for (int index = 0; index < task.size(); index++) {
double day_req
= (Math.ceil((double)task.get(index)
/ (double)(per_day)));
cur_day += day_req;
// If more days required
// than 'd' days so invalid
if (cur_day > d) {
return false;
}
}
// Valid if days are less
// than or equal to 'd'
return cur_day <= d;
}
// Function to find minimum
// task done each day
public static int minimumTask(ArrayList task,
int d)
{
int left = 1;
int right = Integer.MAX_VALUE;
for (int index = 0; index < task.size(); index++) {
right = Math.max(right, task.get(index));
}
// Variable to store answer
int per_day_task = 0;
while (left <= right) {
int mid = left + (right - left) / 2;
// If 'mid' number of task per day
// is valid so store as answer and
// more to first half
if (valid(mid, task, d)) {
per_day_task = mid;
right = mid - 1;
}
else {
left = mid + 1;
}
}
// Print answer
return per_day_task;
}
// Driver Code
public static void main(String[] args)
{
// Input taken
ArrayList task = new ArrayList(
Arrays.asList(3, 4, 7, 15));
int D = 10;
System.out.println(minimumTask(task, D));
}
}
// This code is contributed by Taranpreet
Python
# Python code to implement the approach
import math
# Function to check if
# all the task can be
# completed by 'per_day'
# number of task per day
def valid(per_day, task, d):
# Variable to store days required
# to done all tasks
cur_day = 0
for index in range(0, len(task)):
day_req = math.ceil((task[index]) / (per_day))
cur_day += day_req
# If more days required
# than 'd' days so invalid
if (cur_day > d):
return False
# Valid if days are less
# than or equal to 'd'
return cur_day <= d
# Function to find minimum
# task done each day
def minimumTask(task, d):
left = 1
right = 1e9 + 7
for index in range(0, len(task)):
right = max(right, task[index])
# Variable to store answer
per_day_task = 0
while (left <= right):
mid = left + (right - left) // 2
# If 'mid' number of task per day
# is valid so store as answer and
# more to first half
if (valid(mid, task, d)):
per_day_task = mid
right = mid - 1
else:
left = mid + 1
# Print answer
return math.trunc(per_day_task)
# Driver Code
# Input taken
task = [3, 4, 7, 15]
D = 10
print(minimumTask(task, D))
# This code is contributed by Samim Hossain Mondal.
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function to check if
// all the task can be
// completed by 'per_day'
// number of task per day
public static bool
valid(int per_day, List task, int d)
{
// Variable to store days required
// to done all tasks
int cur_day = 0;
for (int index = 0; index < task.Count; index++) {
double day_req
= (Math.Ceiling((double)task[(index)]
/ (double)(per_day)));
cur_day += (int)day_req;
// If more days required
// than 'd' days so invalid
if (cur_day > d) {
return false;
}
}
// Valid if days are less
// than or equal to 'd'
return cur_day <= d;
}
// Function to find minimum
// task done each day
public static int minimumTask(List task,
int d)
{
int left = 1;
int right = Int32.MaxValue;
for (int index = 0; index < task.Count; index++) {
right = Math.Max(right, task[index]);
}
// Variable to store answer
int per_day_task = 0;
while (left <= right) {
int mid = left + (right - left) / 2;
// If 'mid' number of task per day
// is valid so store as answer and
// more to first half
if (valid(mid, task, d)) {
per_day_task = mid;
right = mid - 1;
}
else {
left = mid + 1;
}
}
// Print answer
return per_day_task;
}
// Driver Code
public static void Main(String []args)
{
// Input taken
List task = new List();
task.Add(3);
task.Add(4);
task.Add(7);
task.Add(15);
int D = 10;
Console.WriteLine(minimumTask(task, D));
}
}
// This code is contributed by code_hunt.
Javascript
4
时间复杂度: O(N * logN)
辅助空间: O(1)