📌  相关文章
📜  将N个给定的木材切成至少K块,可以达到的最大长度

📅  最后修改于: 2021-04-24 16:17:17             🧑  作者: Mango

给定大小为N的wood []数组,表示N块木材的长度和整数K ,则需要从给定的木块中切割至少K个相同长度的块。任务是找到可以获取的这K个木制件的最大可能长度。

例子:

方法:可以使用二进制搜索解决问题。请按照以下步骤解决问题:

  • 从数组wood []中找到最大元素,并将其存储在变量中,例如Max
  • L的值必须位于raneg [1,Max]中。因此,在[1,Max]范围内应用二进制搜索。
  • 初始化两个变量,例如left = 1right = Max,以存储L值所在的范围。
  • 检查是否可以将木材切成K片,每片长度等于(左+右)/ 2 。如果发现是真的,则更新left =(left + right)/ 2
  • 否则,更新right =(left + right)/ 2

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to check if it is possible to cut
// woods into K pieces of length len
bool isValid(int wood[], int N, int len, int K)
{
 
    // Stores count of pieces
    // having length equal to K
    int count = 0;
 
    // Traverse wood[] array
    for (int i = 0; i < N; i++) {
 
        // Update count
        count += wood[i] / len;
    }
    return count >= K;
}
 
// Function to find the maximum value of L
int findMaxLen(int wood[], int N, int K)
{
 
    // Stores minimum possible of L
    int left = 1;
 
    // Stores maximum possible value of L
    int right = *max_element(wood,
                             wood + N);
 
    // Apply binary search over
    // the range [left, right]
    while (left <= right) {
 
        // Stores mid value of
        // left and right
        int mid = left + (right - left) / 2;
 
        // If it is possible to cut woods
        // into K pieces having length
        // of each piece equal to mid
        if (isValid(wood, N, mid,
                    K)) {
 
            // Update left
            left = mid + 1;
        }
        else {
 
            // Update right
            right = mid - 1;
        }
    }
    return right;
}
 
// Driver Code
int main()
{
    int wood[] = { 5, 9, 7 };
    int N = sizeof(wood) / sizeof(wood[0]);
    int K = 4;
    cout << findMaxLen(wood, N, K);
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to check if it is possible
// to cut woods into K pieces of
// length len
static boolean isValid(int wood[], int N,
                       int len, int K)
{
     
    // Stores count of pieces
    // having length equal to K
    int count = 0;
 
    // Traverse wood[] array
    for(int i = 0; i < N; i++)
    {
         
        // Update count
        count += wood[i] / len;
    }
    return count >= K;
}
 
// Function to find the maximum value of L
static int findMaxLen(int wood[], int N,
                      int K)
{
     
    // Stores minimum possible of L
    int left = 1;
 
    // Stores maximum possible value of L
    int right = Arrays.stream(wood).max().getAsInt();
 
    // Apply binary search over
    // the range [left, right]
    while (left <= right)
    {
         
        // Stores mid value of
        // left and right
        int mid = left + (right - left) / 2;
 
        // If it is possible to cut woods
        // into K pieces having length
        // of each piece equal to mid
        if (isValid(wood, N, mid, K))
        {
             
            // Update left
            left = mid + 1;
        }
        else
        {
             
            // Update right
            right = mid - 1;
        }
    }
    return right;
}
 
// Driver Code
public static void main(String[] args)
{
    int wood[] = { 5, 9, 7 };
    int N = wood.length;
    int K = 4;
     
    System.out.print(findMaxLen(wood, N, K));
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 program to implement
# the above approach
 
# Function to check if it is possible to
# cut woods into K pieces of length len
def isValid(wood, N, len, K):
 
    # Stores count of pieces
    # having length equal to K
    count = 0
 
    # Traverse wood[] array
    for i in range(N):
 
        # Update count
        count += wood[i] // len
         
    return (count >= K)
 
# Function to find the maximum value of L
def findMaxLen(wood, N, K):
 
    # Stores minimum possible of L
    left = 1
 
    # Stores maximum possible value of L
    right = max(wood)
 
    # Apply binary search over
    # the range [left, right]
    while (left <= right):
         
        # Stores mid value of
        # left and right
        mid = left + (right - left) // 2
 
        # If it is possible to cut woods
        # into K pieces having length
        # of each piece equal to mid
        if (isValid(wood, N, mid, K)):
 
            # Update left
            left = mid + 1
        else:
             
            # Update right
            right = mid - 1
             
    return right
 
# Driver Code
if __name__ == '__main__':
 
    wood = [ 5, 9, 7 ]
    N = len(wood)
    K = 4
     
    print(findMaxLen(wood, N, K))
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
using System.Linq;
class GFG{
 
// Function to check if it is possible
// to cut woods into K pieces of
// length len
static bool isValid(int []wood, int N,
                    int len, int K)
{   
  // Stores count of pieces
  // having length equal to K
  int count = 0;
 
  // Traverse wood[] array
  for(int i = 0; i < N; i++)
  {
    // Update count
    count += wood[i] / len;
  }
  return count >= K;
}
 
// Function to find the maximum
// value of L
static int findMaxLen(int []wood,
                      int N, int K)
{   
  // Stores minimum possible
  // of L
  int left = 1;
 
  // Stores maximum possible
  // value of L
  int right = wood.Max();
 
  // Apply binary search over
  // the range [left, right]
  while (left <= right)
  {
    // Stores mid value of
    // left and right
    int mid = left +
              (right - left) / 2;
 
    // If it is possible to cut woods
    // into K pieces having length
    // of each piece equal to mid
    if (isValid(wood, N,
                mid, K))
    {
      // Update left
      left = mid + 1;
    }
    else
    {
      // Update right
      right = mid - 1;
    }
  }
  return right;
}
 
// Driver Code
public static void Main(String[] args)
{
  int []wood = {5, 9, 7};
  int N = wood.Length;
  int K = 4;
  Console.Write(findMaxLen(wood,
                           N, K));
}
}
 
// This code is contributed by shikhasingrajput


输出:
4












时间复杂度: O(N * Log 2 M),其中M是给定数组的最大元素
辅助空间: O(1)