📌  相关文章
📜  包含给定数组的所有元素所需的小数组的最小容量

📅  最后修改于: 2021-09-16 11:01:49             🧑  作者: Mango

给定的正整数和一个值K阵列,该任务是清空在阵列小于或等于k小阵列,使得每个小阵只能包含在从给定阵列的单个时隙/索引最大P个元素。找出 P 的最小值。
例子:

方法:为了解决这个问题,我们需要二分搜索答案。

  1. 首先,我们将下限设置为 1,将上限设置为给定数组的最大值。
  2. 现在,我们可以在这个范围内执行二分查找。对于特定的容量值,我们计算需要包含项目中所有值的小数组的数量。
  3. 如果所需的小数组数量大于 K,则答案肯定更大,因此我们修剪搜索的左侧。如果它小于或等于 K,我们将该值保留为潜在答案并修剪搜索的右侧。

下面是上述方法的实现。

C++
// C++ program to find the Minimum
// capacity of small arrays needed
// to contain all element of
// the given array
#include 
using namespace std;
 
 
// Function returns the value
// of Minimum capacity needed
int MinimumCapacity(vector arr,
                    int K)
{
    // Initializing maximum
    // value
    int maxVal = arr[0];
 
    // Finding maximum value
    // in arr
    for (auto x : arr)
        maxVal = max(maxVal, x);
 
    int l = 1, r = maxVal, m;
    int ans, req;
 
    // Binary Search the answer
    while (l <= r)
    {
 
        // m is the mid-point
        // of the range
        m = l + (r - l) / 2;
 
        req = 0;
 
        // Finding the total number of
        // arrays needed to completely
        // contain the items array
        // with P = req
        for (auto x : arr)
            req += x / m + (x % m > 0);
         
        // If the required number of
        // arrays is more than K, it
        // means we need to increase
        // the value of P
        if (req > K)
            l = m + 1;
 
        else
            // If the required number of
            // arrays is less than or equal
            // to K, it means this is a
            // possible answer and we go to
            // check if any smaller possible
            // value exists for P
            ans = m, r = m - 1;
    }
 
    return ans;
}
     
// Driver Code
int main()
{
 
    // Given array
    vector arr = { 1, 2, 3, 4, 5 };
 
    // Number of available small arrays
    int K = 7;
 
    cout << MinimumCapacity(arr, K);
 
    return 0;
}


Java
// Java program to find the Minimum
// capacity of small arrays needed
// to contain all element of
// the given array
class GFG{
 
// Function returns the value
// of Minimum capacity needed
static int MinimumCapacity(int []arr,
                           int K)
{
    // Initializing maximum
    // value
    int maxVal = arr[0];
 
    // Finding maximum value
    // in arr
    for (int x : arr)
        maxVal = Math.max(maxVal, x);
 
    int l = 1, r = maxVal, m;
    int ans = 0, req;
 
    // Binary Search the answer
    while (l <= r)
    {
 
        // m is the mid-point
        // of the range
        m = l + (r - l) / 2;
 
        req = 0;
 
        // Finding the total number of
        // arrays needed to completely
        // contain the items array
        // with P = req
        for (int x : arr)
            req += x / m + (x % m > 0 ? 1 : 0);
         
        // If the required number of
        // arrays is more than K, it
        // means we need to increase
        // the value of P
        if (req > K)
            l = m + 1;
 
        else
        {
            // If the required number of
            // arrays is less than or equal
            // to K, it means this is a
            // possible answer and we go to
            // check if any smaller possible
            // value exists for P
            ans = m;
            r = m - 1;
        }
    }
 
    return ans;
}
     
// Driver Code
public static void main(String[] args)
{
 
    // Given array
    int []arr = { 1, 2, 3, 4, 5 };
 
    // Number of available small arrays
    int K = 7;
 
    System.out.print(MinimumCapacity(arr, K));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to find the Minimum
# capacity of small arrays needed
# to contain all element of
# the given array
 
# Function returns the value
# of Minimum capacity needed
def MinimumCapacity(arr, K):
 
    # Initializing maximum
    # value
    maxVal = arr[0]
 
    # Finding maximum value
    # in arr
    for x in arr:
        maxVal = max(maxVal, x)
 
    l = 1
    r = maxVal
    m = 0
    ans = 0
    req = 0
 
    # Binary Search the answer
    while l <= r:
 
        # m is the mid-point
        # of the range
        m = l + (r - l) // 2
 
        req = 0
 
        # Finding the total number of
        # arrays needed to completely
        # contain the items array
        # with P = req
        for x in arr:
            req += x // m + (x % m > 0)
         
        # If the required number of
        # arrays is more than K, it
        # means we need to increase
        # the value of P
        if req > K:
            l = m + 1
 
        else:
             
            # If the required number of
            # arrays is less than or equal
            # to K, it means this is a
            # possible answer and we go to
            # check if any smaller possible
            # value exists for P
            ans = m
            r = m - 1
 
    return ans
     
#Driver Code
# Given array
arr = [ 1, 2, 3, 4, 5 ]
 
# Number of available small arrays
K = 7
print(MinimumCapacity(arr, K))
 
# This code is contributed by divyamohan123


C#
// C# program to find the minimum
// capacity of small arrays needed
// to contain all element of
// the given array
using System;
 
class GFG{
 
// Function returns the value
// of minimum capacity needed
static int MinimumCapacity(int []arr,
                           int K)
{
     
    // Initializing maximum
    // value
    int maxVal = arr[0];
 
    // Finding maximum value
    // in arr
    foreach (int x in arr)
        maxVal = Math.Max(maxVal, x);
 
    int l = 1, r = maxVal, m;
    int ans = 0, req;
 
    // Binary Search the answer
    while (l <= r)
    {
 
        // m is the mid-point
        // of the range
        m = l + (r - l) / 2;
 
        req = 0;
         
        // Finding the total number of
        // arrays needed to completely
        // contain the items array
        // with P = req
        foreach (int x in arr)
            req += x / m + (x % m > 0 ? 1 : 0);
         
        // If the required number of
        // arrays is more than K, it
        // means we need to increase
        // the value of P
        if (req > K)
            l = m + 1;
 
        else
        {
             
            // If the required number of
            // arrays is less than or equal
            // to K, it means this is a
            // possible answer and we go to
            // check if any smaller possible
            // value exists for P
            ans = m;
            r = m - 1;
        }
    }
    return ans;
}
     
// Driver Code
public static void Main(String[] args)
{
 
    // Given array
    int []arr = { 1, 2, 3, 4, 5 };
 
    // Number of available small arrays
    int K = 7;
 
    Console.Write(MinimumCapacity(arr, K));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
3

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程