给定一个数组arr[]由N 个正整数组成,表示N根绳索的长度和一个正整数K ,任务是通过切割任意数量的绳索来找到频率至少为 K的绳索的最大长度件。
例子:
Input: arr[] = {5, 2, 7, 4, 9}, K = 5
Output: 4
Explanation:
Below are the possible cutting of the ropes:
- arr[0](= 5) is cutted into {4, 1}.
- arr[2](= 7) is cutted into {4, 3}.
- arr[4](= 9) is cutted into {4, 4, 1}.
After the above combinations of cuts, the maximum length is 4, which is of frequency at least(K = 5).
Input: arr[] = {1, 2, 3, 4, 9}, K = 6
Output: 2
方法:给定的问题可以通过使用二分搜索来解决。请按照以下步骤解决问题:
- 初始化 3 个变量,例如低为1 ,高为数组 arr[] 的最大值,以及ans为-1 ,以存储二分搜索的左边界右边界并存储K绳索的最大可能长度。
- 迭代直到low小于或等于high并执行以下步骤:
- 找到范围[low, high]的中间值并将其存储在一个变量中,比如mid 。
- 遍历数组arr[]并找到可以通过切割绳索获得的长度为mid的绳索的数量,并将其存储在变量中,例如count 。
- 如果count的值至少为K ,则将mid的值更新为ans并将low的值更新为(mid + 1) 。
- 否则,将high的值更新为(mid – 1) 。
- 完成这些步骤后,打印ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum size
// of ropes having frequency at least
// K by cutting the given ropes
int maximumSize(int a[], int k, int n)
{
// Stores the left and
// the right boundaries
int low = 1;
int high = *max_element(a, a + n);
// Stores the maximum length
// of rope possible
int ans = -1;
// Iterate while low is less
// than or equal to high
while (low <= high) {
// Stores the mid value of
// the range [low, high]
int mid = low + (high - low) / 2;
// Stores the count of ropes
// of length mid
int count = 0;
// Traverse the array arr[]
for (int c = 0; c < n; c++) {
count += a / mid;
}
// If count is at least K
if (count >= k) {
// Assign mid to ans
ans = mid;
// Update the value
// of low
low = mid + 1;
}
// Otherwise, update the
// value of high
else {
high = mid - 1;
}
}
// Return the value of ans
return ans;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4, 9 };
int K = 6;
int n = sizeof(arr) / sizeof(arr[0]);
cout << (maximumSize(arr, K, n));
}
// This code is contributed by ukasp
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to find the maximum size
// of ropes having frequency at least
// K by cutting the given ropes
static int maximumSize(Integer[] a,
int k)
{
// Stores the left and
// the right boundaries
int low = 1;
int high = Collections.max(
Arrays.asList(a));
// Stores the maximum length
// of rope possible
int ans = -1;
// Iterate while low is less
// than or equal to high
while (low <= high) {
// Stores the mid value of
// the range [low, high]
int mid = low + (high - low) / 2;
// Stores the count of ropes
// of length mid
int count = 0;
// Traverse the array arr[]
for (int c = 0;
c < a.length; c++) {
count += a / mid;
}
// If count is at least K
if (count >= k) {
// Assign mid to ans
ans = mid;
// Update the value
// of low
low = mid + 1;
}
// Otherwise, update the
// value of high
else {
high = mid - 1;
}
}
// Return the value of ans
return ans;
}
// Driver Code
public static void main(String[] args)
{
Integer[] arr = { 1, 2, 3, 4, 9 };
int K = 6;
System.out.println(
maximumSize(arr, K));
}
}
Python3
# Python program for the above approach
# Function to find the maximum size
# of ropes having frequency at least
# K by cutting the given ropes
def maximumSize(a, k):
# Stores the left and
# the right boundaries
low = 1
high = max(a)
# Stores the maximum length
# of rope possible
ans = -1
# Iterate while low is less
# than or equal to high
while (low <= high):
# Stores the mid value of
# the range [low, high]
mid = low + (high - low) // 2
# Stores the count of ropes
# of length mid
count = 0
# Traverse the array arr[]
for c in range(len(a)):
count += a // mid
# If count is at least K
if (count >= k):
# Assign mid to ans
ans = mid
# Update the value
# of low
low = mid + 1
# Otherwise, update the
# value of high
else:
high = mid - 1
# Return the value of ans
return ans
# Driver Code
if __name__ == '__main__':
arr = [1, 2, 3, 4, 9]
K = 6
print(maximumSize(arr, K))
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
using System.Linq;
class GFG{
// Function to find the maximum size
// of ropes having frequency at least
// K by cutting the given ropes
static int maximumSize(int[] a, int k)
{
// Stores the left and
// the right boundaries
int low = 1;
int high = a.Max();
// Stores the maximum length
// of rope possible
int ans = -1;
// Iterate while low is less
// than or equal to high
while (low <= high)
{
// Stores the mid value of
// the range [low, high]
int mid = low + (high - low) / 2;
// Stores the count of ropes
// of length mid
int count = 0;
// Traverse the array []arr
for(int c = 0;
c < a.Length; c++)
{
count += a / mid;
}
// If count is at least K
if (count >= k)
{
// Assign mid to ans
ans = mid;
// Update the value
// of low
low = mid + 1;
}
// Otherwise, update the
// value of high
else
{
high = mid - 1;
}
}
// Return the value of ans
return ans;
}
// Driver Code
public static void Main(String[] args)
{
int[] arr = { 1, 2, 3, 4, 9 };
int K = 6;
Console.WriteLine(
maximumSize(arr, K));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
2
时间复杂度: O(N * log N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。