📜  D 天内运送包裹的能力

📅  最后修改于: 2021-09-05 08:51:18             🧑  作者: Mango

给定一个数组arr[]N 个正整数组成,表示N 个项目重量和一个正整数D ,任务是找到一艘船的最小承重能力(比如K )在D天内运送所有重量,使得订单船上装载的重量按arr[] 中数组元素的顺序排列,船舶每天装载的重量总量为K

例子:

方法:给定的问题可以通过使用贪心技术和二分搜索来解决。问题的单调性可以看出,如果所有包裹都可以在D天内以容量K成功发货,那么它们肯定可以以任何大于K 的容量发货。请按照以下步骤解决问题:

  • 初始化一个变量,比如ans-1来存储所需的船的最小容量。
  • 用给定数组中的最大元素和数组的总和分别初始化两个变量,比如se ,这表示搜索空间的下限和上限。
  • 迭代直到s的值小于或等于e ,并执行以下步骤:
    • 初始化一个变量,比如mid(s + e)/2
    • 检查是否可以在允许的最大容量为mid 的D天内运送所有包裹。如果发现为true ,则将ans的值更新为mid ,将e的值更新为(mid – 1)
    • 否则,将s的值更新为(mid + 1)
  • 完成以上步骤后,打印ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if the weights
// can be delivered in D days or not
bool isValid(int weight[], int n,
             int D, int mx)
{
    // Stores the count of days required
    // to ship all the weights if the
    // maximum capacity is mx
    int st = 1;
    int sum = 0;
 
    // Traverse all the weights
    for (int i = 0; i < n; i++) {
        sum += weight[i];
 
        // If total weight is more than
        // the maximum capacity
        if (sum > mx) {
            st++;
            sum = weight[i];
        }
 
        // If days are more than D,
        // then return false
        if (st > D)
            return false;
    }
 
    // Return true for the days < D
    return true;
}
 
// Function to find the least weight
// capacity of a boat to ship all the
// weights within D days
void shipWithinDays(int weight[], int D,
                    int n)
{
    // Stores the total weights to
    // be shipped
    int sum = 0;
 
    // Find the sum of weights
    for (int i = 0; i < n; i++)
        sum += weight[i];
 
    // Stores the maximum weight in the
    // array that has to be shipped
    int s = weight[0];
    for (int i = 1; i < n; i++) {
        s = max(s, weight[i]);
    }
 
    // Store the ending value for
    // the search space
    int e = sum;
 
    // Store the required result
    int res = -1;
 
    // Perform binary search
    while (s <= e) {
 
        // Store the middle value
        int mid = s + (e - s) / 2;
 
        // If mid can be shipped, then
        // update the result and end
        // value of the search space
        if (isValid(weight, n, D, mid)) {
            res = mid;
            e = mid - 1;
        }
 
        // Search for minimum value
        // in the right part
        else
            s = mid + 1;
    }
 
    // Print the result
    cout << res;
}
 
// Driver Code
int main()
{
    int weight[] = { 9, 8, 10 };
    int D = 3;
    int N = sizeof(weight) / sizeof(weight[0]);
    shipWithinDays(weight, D, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
   
// Function to check if the weights
// can be delivered in D days or not
static boolean isValid(int[] weight, int n,
                       int D, int mx)
{
     
    // Stores the count of days required
    // to ship all the weights if the
    // maximum capacity is mx
    int st = 1;
    int sum = 0;
 
    // Traverse all the weights
    for(int i = 0; i < n; i++)
    {
        sum += weight[i];
 
        // If total weight is more than
        // the maximum capacity
        if (sum > mx)
        {
            st++;
            sum = weight[i];
        }
 
        // If days are more than D,
        // then return false
        if (st > D)
            return false;
    }
 
    // Return true for the days < D
    return true;
}
 
// Function to find the least weight
// capacity of a boat to ship all the
// weights within D days
static void shipWithinDays(int[] weight, int D, int n)
{
     
    // Stores the total weights to
    // be shipped
    int sum = 0;
 
    // Find the sum of weights
    for(int i = 0; i < n; i++)
        sum += weight[i];
 
    // Stores the maximum weight in the
    // array that has to be shipped
    int s = weight[0];
    for(int i = 1; i < n; i++)
    {
        s = Math.max(s, weight[i]);
    }
 
    // Store the ending value for
    // the search space
    int e = sum;
 
    // Store the required result
    int res = -1;
 
    // Perform binary search
    while (s <= e)
    {
         
        // Store the middle value
        int mid = s + (e - s) / 2;
 
        // If mid can be shipped, then
        // update the result and end
        // value of the search space
        if (isValid(weight, n, D, mid))
        {
            res = mid;
            e = mid - 1;
        }
 
        // Search for minimum value
        // in the right part
        else
            s = mid + 1;
    }
 
    // Print the result
    System.out.println(res);
}
 
// Driver Code
public static void main(String[] args)
{
     
    int[] weight = { 9, 8, 10 };
    int D = 3;
    int N = weight.length;
     
    shipWithinDays(weight, D, N);
}
}
 
// This code is contributed by Dharanendra L V.


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if the weights
// can be delivered in D days or not
static bool isValid(int[] weight, int n,
                    int D, int mx)
{
     
    // Stores the count of days required
    // to ship all the weights if the
    // maximum capacity is mx
    int st = 1;
    int sum = 0;
 
    // Traverse all the weights
    for(int i = 0; i < n; i++)
    {
        sum += weight[i];
 
        // If total weight is more than
        // the maximum capacity
        if (sum > mx)
        {
            st++;
            sum = weight[i];
        }
 
        // If days are more than D,
        // then return false
        if (st > D)
            return false;
    }
 
    // Return true for the days < D
    return true;
}
 
// Function to find the least weight
// capacity of a boat to ship all the
// weights within D days
static void shipWithinDays(int[] weight, int D, int n)
{
     
    // Stores the total weights to
    // be shipped
    int sum = 0;
 
    // Find the sum of weights
    for(int i = 0; i < n; i++)
        sum += weight[i];
 
    // Stores the maximum weight in the
    // array that has to be shipped
    int s = weight[0];
    for(int i = 1; i < n; i++)
    {
        s = Math.Max(s, weight[i]);
    }
 
    // Store the ending value for
    // the search space
    int e = sum;
 
    // Store the required result
    int res = -1;
 
    // Perform binary search
    while (s <= e)
    {
         
        // Store the middle value
        int mid = s + (e - s) / 2;
 
        // If mid can be shipped, then
        // update the result and end
        // value of the search space
        if (isValid(weight, n, D, mid))
        {
            res = mid;
            e = mid - 1;
        }
 
        // Search for minimum value
        // in the right part
        else
            s = mid + 1;
    }
 
    // Print the result
    Console.WriteLine(res);
}
 
// Driver Code
public static void Main()
{
    int[] weight = { 9, 8, 10 };
    int D = 3;
    int N = weight.Length;
     
    shipWithinDays(weight, D, N);
}
}
 
// This code is contributed by ukasp


Javascript


输出:
10

时间复杂度: O(N*log(S – M)),其中S数组元素总和, M数组最大元素
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live