📌  相关文章
📜  最小值K,以使得除以K后的数组元素之和不超过S

📅  最后修改于: 2021-05-05 02:25:18             🧑  作者: Mango

给定一个由N个元素组成的数组arr []和一个整数S。任务是找到最小数K ,以使所有元素除以K后,数组元素的总和不超过S。
注意:请考虑整数除法。
例子:

天真的方法:K的所有值从1迭代到数组中的最大元素,然后除以K求和得出数组元素的总和,如果总和不超过S,那么当前值就是答案。这种方法的时间复杂度将是O(M * N) ,其中M是数组中的最大元素。
高效的方法:一种有效的方法是通过对答案执行二进制搜索来找到K的值。对K的值启动二进制搜索,并在其中进行检查以查看总和是否超过K,然后对后半部分或前半部分进行二进制搜索。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the minimum value of k
// that satisfies the given condition
int findMinimumK(int a[], int n, int s)
{
    // Find the maximum element
    int maximum = a[0];
    for (int i = 0; i < n; i++) {
        maximum = max(maximum, a[i]);
    }
 
    // Lowest answer can be 1 and the
    // highest answer can be (maximum + 1)
    int low = 1, high = maximum + 1;
 
    int ans = high;
 
    // Binary search
    while (low <= high) {
 
        // Get the mid element
        int mid = (low + high) / 2;
        int sum = 0;
 
        // Calculate the sum after dividing
        // the array by new K which is mid
        for (int i = 0; i < n; i++) {
            sum += (int)(a[i] / mid);
        }
 
        // Search in the second half
        if (sum > s)
            low = mid + 1;
 
        // First half
        else {
            ans = min(ans, mid);
            high = mid - 1;
        }
    }
 
    return ans;
}
 
// Driver code
int main()
{
    int a[] = { 10, 7, 8, 10, 12, 19 };
    int n = sizeof(a) / sizeof(a[0]);
    int s = 27;
 
    cout << findMinimumK(a, n, s);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
    // Function to return the minimum value of k
    // that satisfies the given condition
    static int findMinimumK(int a[],
                            int n, int s)
    {
        // Find the maximum element
        int maximum = a[0];
         
        for (int i = 0; i < n; i++)
        {
            maximum = Math.max(maximum, a[i]);
        }
     
        // Lowest answer can be 1 and the
        // highest answer can be (maximum + 1)
        int low = 1, high = maximum + 1;
     
        int ans = high;
     
        // Binary search
        while (low <= high)
        {
     
            // Get the mid element
            int mid = (low + high) / 2;
            int sum = 0;
     
            // Calculate the sum after dividing
            // the array by new K which is mid
            for (int i = 0; i < n; i++)
            {
                sum += (int)(a[i] / mid);
            }
     
            // Search in the second half
            if (sum > s)
                low = mid + 1;
     
            // First half
            else
            {
                ans = Math.min(ans, mid);
                high = mid - 1;
            }
        }
        return ans;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int a[] = { 10, 7, 8, 10, 12, 19 };
        int n = a.length;
        int s = 27;
     
        System.out.println(findMinimumK(a, n, s));
    }
}   
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach
 
# Function to return the minimum value of k
# that satisfies the given condition
def findMinimumK(a, n, s):
     
    # Find the maximum element
    maximum = a[0]
    for i in range(n):
        maximum = max(maximum, a[i])
 
    # Lowest answer can be 1 and the
    # highest answer can be (maximum + 1)
    low = 1
    high = maximum + 1
 
    ans = high
 
    # Binary search
    while (low <= high):
 
        # Get the mid element
        mid = (low + high) // 2
        sum = 0
 
        # Calculate the sum after dividing
        # the array by new K which is mid
        for i in range(n):
            sum += (a[i] // mid)
 
        # Search in the second half
        if (sum > s):
            low = mid + 1
 
        # First half
        else:
            ans = min(ans, mid)
            high = mid - 1
 
    return ans
 
# Driver code
a = [10, 7, 8, 10, 12, 19]
n = len(a)
s = 27
 
print(findMinimumK(a, n, s))
 
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
    // Function to return the minimum value of k
    // that satisfies the given condition
    static int findMinimumK(int []a,
                            int n, int s)
    {
        // Find the maximum element
        int maximum = a[0];
         
        for (int i = 0; i < n; i++)
        {
            maximum = Math.Max(maximum, a[i]);
        }
     
        // Lowest answer can be 1 and the
        // highest answer can be (maximum + 1)
        int low = 1, high = maximum + 1;
     
        int ans = high;
     
        // Binary search
        while (low <= high)
        {
     
            // Get the mid element
            int mid = (low + high) / 2;
            int sum = 0;
     
            // Calculate the sum after dividing
            // the array by new K which is mid
            for (int i = 0; i < n; i++)
            {
                sum += (int)(a[i] / mid);
            }
     
            // Search in the second half
            if (sum > s)
                low = mid + 1;
     
            // First half
            else
            {
                ans = Math.Min(ans, mid);
                high = mid - 1;
            }
        }
        return ans;
    }
     
    // Driver code
    public static void Main ()
    {
        int []a = { 10, 7, 8, 10, 12, 19 };
        int n = a.Length;
        int s = 27;
     
        Console.WriteLine(findMinimumK(a, n, s));
    }
}
 
// This code is contributed by AnkitRai01


输出:
3

时间复杂度: O(N *(log N)),N =数组长度

辅助空间: O(1)