给定一个大小为N的数组wood[] ,代表N块木头的长度和一个整数K ,至少需要从给定的木块中切割出K块相同长度的木头。任务是找到可以获得的这K 个木块的最大可能长度。
例子:
Input: wood[] = {5, 9, 7}, K = 3
Output: 5
Explanation:
Cut arr[0] = 5 = 5
Cut arr[1] = 9 = 5 + 4
Cut arr[2] = 5 = 5 + 2
Therefore, the maximum length that can be obtained by cutting the woods into 3 pieces is 5.
Input: wood[] = {5, 9, 7}, K = 4
Output: 4
Explanation:
Cut arr[0] = 5 = 4 + 1
Cut arr[1] = 9 = 2 * 4 + 1
Cut arr[2] = 7 = 4 + 3
Therefore, the maximum length that can be obtained by cutting the woods into 4 pieces is 4.
方法:该问题可以使用二分搜索来解决。请按照以下步骤解决问题:
- 从数组wood[] 中找到最大元素并将其存储在一个变量中,比如Max 。
- L的值必须位于范围[1, Max] 中。因此,在[1, Max]范围内应用二分搜索。
- 初始化两个变量,例如left = 1和right = 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
Javascript
4
时间复杂度: O(N * Log 2 M),其中 M 是给定数组的最大元素
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live