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

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

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

天真的方法:迭代从1到数组中最大元素的所有K值,然后通过除以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


Javascript


输出:
3

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

辅助空间: O(1)

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