📌  相关文章
📜  最小化大小为 K 的 N 个子数组的最大元素

📅  最后修改于: 2021-09-06 17:46:21             🧑  作者: Mango

给定一个数组arr[]和两个整数NK ,任务是选择大小为 K 的非重叠 N 个子数组,使得所有子数组的最大元素最小。
注意:如果不可能选择 N 个这样的子数组,则返回 -1。
例子:

方法:这个想法是使用二进制搜索。下面是二分查找的示意图:

  • 搜索空间:因为我们必须从 N 个子数组中找到最大元素,它是数组中的元素之一。因此,搜索空间将是数组的最小元素到最大元素。
  • 二进制搜索函数:对分检索的函数是找到K-大小的数组可能的计数与所有元素小于给定数目,这将是搜索空间的中部。
  • 左搜索空间:当 K 大小的可能子数组的个数大于等于 N 时,则可能的答案位于左搜索空间。
  • 右搜索空间:当 K 大小的可能子数组的数量小于 N 时,则可能扫描的答案位于右搜索空间。

下面是上述方法的实现:

C++
// C++ implementation to choose
// N subarrays of size K such that
// the maximum element of
// subarrays is minimum
 
#include 
using namespace std;
 
// Function to choose
// N subarrays of size K such that
// the maximum element of
// subarrays is minimum
int minDays(vector& arr,
                int n, int k)
{
    int l = arr.size(),
left = 1, right = 1e9;
 
    // Condition to check if it
    // is not possible to choose k
    // sized N subarrays
    if (n * k > l)
        return -1;
 
    // Using binary search
    while (left < right) {
 
        // calculating mid
        int mid = (left + right) / 2,
                 cnt = 0, product = 0;
                  
        // Loop to find the count of the
        // K sized subarrays possible with
        // elements less than  mid
        for (int j = 0; j < l; ++j) {
            if (arr[j] > mid) {
                cnt = 0;
            }
            else if (++cnt >= k) {
                product++;
                cnt = 0;
            }
        }
     
        // Condition to check if the
        // answer is in right subarray
        if (product < n) {
            left = mid + 1;
        }
        else {
            right = mid;
        }
    }
    return left;
}
 
// Driver Code
int main()
{
    vector arr{ 1, 10, 3, 10, 2 };
    int n = 3, k = 1;
     
    // Function Call
    cout << minDays(arr, n, k) << endl;
    return 0;
}


Java
// Java implementation to choose
// N subarrays of size K such that
// the maximum element of
// subarrays is minimum
class GFG{
 
// Function to choose
// N subarrays of size K such that
// the maximum element of
// subarrays is minimum
static int minDays(int []arr,
                   int n, int k)
{
    int l = arr.length,
        left = 1, right = (int) 1e9;
 
    // Condition to check if it
    // is not possible to choose k
    // sized N subarrays
    if (n * k > l)
        return -1;
 
    // Using binary search
    while (left < right)
    {
 
        // calculating mid
        int mid = (left + right) / 2,
                   cnt = 0, product = 0;
                 
        // Loop to find the count of the
        // K sized subarrays possible with
        // elements less than mid
        for (int j = 0; j < l; ++j)
        {
            if (arr[j] > mid)
            {
                cnt = 0;
            }
            else if (++cnt >= k)
            {
                product++;
                cnt = 0;
            }
        }
     
        // Condition to check if the
        // answer is in right subarray
        if (product < n)
        {
            left = mid + 1;
        }
        else
        {
            right = mid;
        }
    }
    return left;
}
 
// Driver Code
public static void main(String[] args)
{
    int []arr = {1, 10, 3, 10, 2};
    int n = 3, k = 1;
     
    // Function Call
    System.out.print(minDays(arr, n, k) + "\n");
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 implementation to choose
# N subarrays of size K such that
# the maximum element of
# subarrays is minimum
  
# Function to choose
# N subarrays of size K such that
# the maximum element of
# subarrays is minimum
def minDays(arr, n, k):
 
    l = len(arr)
    left = 1
    right = 1e9
  
    # Condition to check if it
    # is not possible to choose k
    # sized N subarrays
    if (n * k > l):
        return -1
  
    # Using binary search
    while (left < right):
  
        # calculating mid
        mid = (left + right) // 2
        cnt = 0
        product = 0
                   
        # Loop to find the count of the
        # K sized subarrays possible with
        # elements less than  mid
        for j in range (l):
            if (arr[j] > mid):
                cnt = 0
            else:
                cnt += 1
                if (cnt >= k):
                    product += 1
                    cnt = 0
      
        # Condition to check if the
        # answer is in right subarray
        if (product < n):
            left = mid + 1
        else:
            right = mid
      
    return left
  
# Driver Code
if __name__ == "__main__":
    arr = [1, 10, 3, 10, 2]
    n = 3
    k = 1
      
    # Function Call
    print (int(minDays(arr, n, k)))
     
# This code is contributed by Chitranayal


C#
// C# implementation to choose N 
// subarrays of size K such that
// the maximum element of
// subarrays is minimum
using System;
class GFG{
 
// Function to choose N subarrays 
// of size K such that the maximum
// element of subarrays is minimum
static int minDays(int []arr,
                   int n, int k)
{
    int l = arr.Length;
    int left = 1, right = (int)1e9;
 
    // Condition to check if it
    // is not possible to choose k
    // sized N subarrays
    if (n * k > l)
        return -1;
 
    // Using binary search
    while (left < right)
    {
 
        // Calculating mid
        int mid = (left + right) / 2,
                   cnt = 0, product = 0;
                 
        // Loop to find the count of the
        // K sized subarrays possible with
        // elements less than mid
        for(int j = 0; j < l; ++j)
        {
           if (arr[j] > mid)
           {
               cnt = 0;
           }
           else if (++cnt >= k)
           {
               product++;
               cnt = 0;
           }
        }
     
        // Condition to check if the
        // answer is in right subarray
        if (product < n)
        {
            left = mid + 1;
        }
        else
        {
            right = mid;
        }
    }
    return left;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 10, 3, 10, 2 };
    int n = 3, k = 1;
     
    // Function Call
    Console.Write(minDays(arr, n, k) + "\n");
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:

3

时间复杂度: O(N*logN)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live