📜  找到最大高度以水平切割所有巧克力,以便至少保留 K 量

📅  最后修改于: 2022-05-13 01:56:05.378000             🧑  作者: Mango

找到最大高度以水平切割所有巧克力,以便至少保留 K 量

给定一个由N个巧克力棒高度组成的数组arr[] ,任务是找到对所有巧克力进行水平切割的最大高度,使得巧克力的剩余总量至少为 K。

例子:

方法:给定的问题可以基于二分搜索来解决。

请按照以下步骤解决给定的问题:

  • 分别初始化两个变量,比如lowhigh0和最大数组元素。
  • 迭代直到low <= high并执行以下步骤:
    • 找到mid的值为(low + high)/2
    • 中间高度为M进行水平切割后,找到剩余巧克力的总和。
    • 如果M的值小于K ,则将high的值更新为(mid – 1) 。否则,将low的值更新为(mid + 1)
  • 执行上述步骤后,打印与最终必须切割的最大高度一样的值。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the sum of remaining
// chocolate after making the horizontal
// cut at height mid
int cal(vector arr, int mid)
{
 
    // Stores the sum of chocolates
    int chocolate = 0;
 
    // Traverse the array arr[]
    for (auto i : arr) {
 
        // If the height is at least mid
        if (i >= mid)
            chocolate += i - mid;
    }
 
    // Return the possible sum
    return chocolate;
}
 
// Function to find the maximum horizontal
// cut made to all the chocolates such that
// the sum of the remaining element is
// at least K
int maximumCut(vector arr, int K)
{
 
    // Ranges of Binary Search
    int low = 0;
    int high = *max_element(arr.begin(), arr.end());
 
    // Perform the Binary Search
    while (low <= high) {
        int mid = (low + high) / 2;
 
        // Find the sum of removed after
        // making cut at height mid
        int chocolate = cal(arr, mid);
 
        // If the chocolate removed is
        // same as the chocolate needed
        // then return the height
        if (chocolate == K)
            return mid;
 
        // If the chocolate removed is
        // less than chocolate needed
        // then shift to the left range
        else if (chocolate < K)
            high = mid - 1;
 
        // Otherwise, shift to the right
        // range
        else {
            low = mid + 1;
            if (mid > high)
                high = mid;
        }
    }
 
    // Return the possible cut
    return high;
}
 
// Driver Code
int main()
{
    int N = 4;
    int K = 7;
    vector arr{ 15, 20, 8, 17 };
    cout << (maximumCut(arr, K));
}
 
// This code is contributed by ipg2016107.


Java
// Java program for the above approach
import java.util.*;
import java.util.Arrays;
class GFG {
 
    // Function to find the sum of remaining
    // chocolate after making the horizontal
    // cut at height mid
    static int cal(int arr[], int mid)
    {
 
        // Stores the sum of chocolates
        int chocolate = 0;
 
        // Traverse the array arr[]
        for (int i = 0; i < arr.length; i++) {
 
            // If the height is at least mid
            if (arr[i] >= mid)
                chocolate += arr[i] - mid;
        }
 
        // Return the possible sum
        return chocolate;
    }
 
    // Function to find the maximum horizontal
    // cut made to all the chocolates such that
    // the sum of the remaining element is
    // at least K
    static int maximumCut(int arr[], int K)
    {
 
        // Ranges of Binary Search
        int low = 0;
        int high = Arrays.stream(arr).max().getAsInt();
 
        // Perform the Binary Search
        while (low <= high) {
            int mid = (low + high) / 2;
 
            // Find the sum of removed after
            // making cut at height mid
            int chocolate = cal(arr, mid);
 
            // If the chocolate removed is
            // same as the chocolate needed
            // then return the height
            if (chocolate == K)
                return mid;
 
            // If the chocolate removed is
            // less than chocolate needed
            // then shift to the left range
            else if (chocolate < K)
                high = mid - 1;
 
            // Otherwise, shift to the right
            // range
            else {
                low = mid + 1;
                if (mid > high)
                    high = mid;
            }
        }
 
        // Return the possible cut
        return high;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int K = 7;
        int arr[] = { 15, 20, 8, 17 };
        System.out.println(maximumCut(arr, K));
    }
}
 
// This code is contributed by ukasp.


Python3
# Python program for the above approach
 
# Function to find the sum of remaining
# chocolate after making the horizontal
# cut at height mid
 
 
def cal(arr, mid):
 
      # Stores the sum of chocolates
    chocolate = 0
 
    # Traverse the array arr[]
    for i in arr:
 
          # If the height is at least mid
        if i >= mid:
            chocolate += i - mid
 
    # Return the possible sum
    return chocolate
 
# Function to find the maximum horizontal
# cut made to all the chocolates such that
# the sum of the remaining element is
# at least K
 
 
def maximumCut(arr, K):
 
      # Ranges of Binary Search
    low = 0
    high = max(arr)
 
    # Perform the Binary Search
    while low <= high:
        mid = (low + high) // 2
 
        # Find the sum of removed after
        # making cut at height mid
        chocolate = cal(arr, mid)
 
        # If the chocolate removed is
        # same as the chocolate needed
        # then return the height
        if chocolate == K:
            return mid
 
        # If the chocolate removed is
        # less than chocolate needed
        # then shift to the left range
        elif chocolate < K:
            high = mid - 1
 
        # Otherwise, shift to the right
        # range
        else:
            low = mid + 1
            if mid > high:
                high = mid
 
    # Return the possible cut
    return high
 
 
# Driver Code
N, K = 4, 7
arr = [15, 20, 8, 17]
 
print(maximumCut(arr, K))


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
using System.Linq;
 
class GFG {
 
    // Function to find the sum of remaining
    // chocolate after making the horizontal
    // cut at height mid
    static int cal(List arr, int mid)
    {
 
        // Stores the sum of chocolates
        int chocolate = 0;
 
        // Traverse the array arr[]
        foreach(int i in arr)
        {
 
            // If the height is at least mid
            if (i >= mid)
                chocolate += i - mid;
        }
 
        // Return the possible sum
        return chocolate;
    }
 
    // Function to find the maximum horizontal
    // cut made to all the chocolates such that
    // the sum of the remaining element is
    // at least K
    static int maximumCut(List arr, int K)
    {
 
        // Ranges of Binary Search
        int low = 0;
        int high = arr.Max();
 
        // Perform the Binary Search
        while (low <= high) {
            int mid = (low + high) / 2;
 
            // Find the sum of removed after
            // making cut at height mid
            int chocolate = cal(arr, mid);
 
            // If the chocolate removed is
            // same as the chocolate needed
            // then return the height
            if (chocolate == K)
                return mid;
 
            // If the chocolate removed is
            // less than chocolate needed
            // then shift to the left range
            else if (chocolate < K)
                high = mid - 1;
 
            // Otherwise, shift to the right
            // range
            else {
                low = mid + 1;
                if (mid > high)
                    high = mid;
            }
        }
 
        // Return the possible cut
        return high;
    }
 
    // Driver Code
    public static void Main()
    {
        int K = 7;
        List arr = new List() { 15, 20, 8, 17 };
        Console.Write(maximumCut(arr, K));
    }
}
 
// This code is contributed by SURENDRA_GANGWAR.


Javascript


输出
15

时间复杂度: O(N*log N)
辅助空间: O(1)