给定的整数k和数组高度[]其中高度[i]表示在森林第i树的高度。任务是从地面切出X高度,以便准确收集K单位木材。如果不可能,则打印-1,否则打印X。
例子:
Input: height[] = {1, 2, 1, 2}, K = 2
Output: 1
Make a cut at height 1, the updated array will be {1, 1, 1, 1} and
the collected wood will be {0, 1, 0, 1} i.e. 0 + 1 + 0 + 1 = 2.
Input: height = {1, 1, 2, 2}, K = 1
Output: -1
方法:可以使用二进制搜索解决此问题。
- 对树木的高度进行排序。
- 进行切割的最低高度为0 ,最高为所有树木中的最大高度。因此,设置low = 0和high = max(height [i]) 。
- 从低到高重复以下步骤:
- 设置中=低+((高–低)/ 2) 。
- 计数如果切割是在中间高度由可以回收的木材量,并将其存储在收集的变量。
- 如果收集= K则是中期的答案。
- 如果collecetd> K,则更新低=中+ 1,因为需要在高于当前高度的高度进行切割
- 否则更新高=中– 1,因为需要在较低的高度进行切割。
- 如果找不到中间值,则打印-1 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
#include
using namespace std;
// Function to return the amount of wood
// collected if the cut is made at height m
int woodCollected(int height[], int n, int m)
{
int sum = 0;
for (int i = n - 1; i >= 0; i--) {
if (height[i] - m <= 0)
break;
sum += (height[i] - m);
}
return sum;
}
// Function that returns Height at
// which cut should be made
int collectKWood(int height[], int n, int k)
{
// Sort the heights of the trees
sort(height, height + n);
// The minimum and the maximum
// cut that can be made
int low = 0, high = height[n - 1];
// Binary search to find the answer
while (low <= high) {
int mid = low + ((high - low) / 2);
// The amount of wood collected
// when cut is made at the mid
int collected = woodCollected(height, n, mid);
// If the current collected wood is
// equal to the required amount
if (collected == k)
return mid;
// If it is more than the required amount
// then the cut needs to be made at a
// height higher than the current height
if (collected > k)
low = mid + 1;
// Else made the cut at a lower height
else
high = mid - 1;
}
return -1;
}
// Driver code
int main()
{
int height[] = { 1, 2, 1, 2 };
int n = sizeof(height) / sizeof(height[0]);
int k = 2;
cout << collectKWood(height, n, k);
return 0;
}
Java
// Java implementation of the approach
import java.util.Arrays;
class GFG
{
static int[] height = new int[]{ 1, 2, 1, 2 };
// Function to return the amount of wood
// collected if the cut is made at height m
public static int woodCollected(int n, int m)
{
int sum = 0;
for (int i = n - 1; i >= 0; i--)
{
if (height[i] - m <= 0)
break;
sum += (height[i] - m);
}
return sum;
}
// Function that returns Height at
// which cut should be made
public static int collectKWood(int n, int k)
{
// Sort the heights of the trees
Arrays.sort(height);
// The minimum and the maximum
// cut that can be made
int low = 0, high = height[n - 1];
// Binary search to find the answer
while (low <= high)
{
int mid = low + ((high - low) / 2);
// The amount of wood collected
// when cut is made at the mid
int collected = woodCollected(n, mid);
// If the current collected wood is
// equal to the required amount
if (collected == k)
return mid;
// If it is more than the required amount
// then the cut needs to be made at a
// height higher than the current height
if (collected > k)
low = mid + 1;
// Else made the cut at a lower height
else
high = mid - 1;
}
return -1;
}
// Driver code
public static void main(String[] args)
{
int k = 2;
int n = height.length;
System.out.print(collectKWood(n,k));
}
}
// This code is contributed by Sanjit_Prasad
Python3
# Python3 implementation of the approach
# Function to return the amount of wood
# collected if the cut is made at height m
def woodCollected(height, n, m):
sum = 0
for i in range(n - 1, -1, -1):
if (height[i] - m <= 0):
break
sum += (height[i] - m)
return sum
# Function that returns Height at
# which cut should be made
def collectKWood(height, n, k):
# Sort the heights of the trees
height = sorted(height)
# The minimum and the maximum
# cut that can be made
low = 0
high = height[n - 1]
# Binary search to find the answer
while (low <= high):
mid = low + ((high - low) // 2)
# The amount of wood collected
# when cut is made at the mid
collected = woodCollected(height, n, mid)
# If the current collected wood is
# equal to the required amount
if (collected == k):
return mid
# If it is more than the required amount
# then the cut needs to be made at a
# height higher than the current height
if (collected > k):
low = mid + 1
# Else made the cut at a lower height
else:
high = mid - 1
return -1
# Driver code
height = [1, 2, 1, 2]
n = len(height)
k = 2
print(collectKWood(height, n, k))
# This code is contributed by Mohit Kumar
C#
// C# implementation of the approach
using System;
using System.Collections;
class GFG
{
static int[] height = { 1, 2, 1, 2 };
// Function to return the amount of wood
// collected if the cut is made at height m
public static int woodCollected(int n, int m)
{
int sum = 0;
for (int i = n - 1; i >= 0; i--)
{
if (height[i] - m <= 0)
break;
sum += (height[i] - m);
}
return sum;
}
// Function that returns Height at
// which cut should be made
public static int collectKWood(int n, int k)
{
// Sort the heights of the trees
Array.Sort(height);
// The minimum and the maximum
// cut that can be made
int low = 0, high = height[n - 1];
// Binary search to find the answer
while (low <= high)
{
int mid = low + ((high - low) / 2);
// The amount of wood collected
// when cut is made at the mid
int collected = woodCollected(n, mid);
// If the current collected wood is
// equal to the required amount
if (collected == k)
return mid;
// If it is more than the required amount
// then the cut needs to be made at a
// height higher than the current height
if (collected > k)
low = mid + 1;
// Else made the cut at a lower height
else
high = mid - 1;
}
return -1;
}
// Driver code
public static void Main()
{
int k = 2;
int n = height.Length;
Console.WriteLine(collectKWood(n,k));
}
}
// This code is contributed by AnkitRai01
输出:
1