📌  相关文章
📜  在K个连续子数组的最小值中最大化最大值

📅  最后修改于: 2021-04-21 22:16:03             🧑  作者: Mango

给定一个整数K和一个数组arr [] ,任务是将数组arr []分成K个连续的子数组,以K个连续的子数组的最小值中找到最大值的最大可能值

例子:

方法:解决方案可以分为3种可能的情况:

  1. 当K = 1时,在这种情况下,答案始终等于数组的最小值,因为该数组仅拆分为一个子数组,即数组本身。
  2. 当K≥3时:在这种情况下,答案始终等于数组的最大值。当必须将数组分为3个或更多段时,请始终保持一个段仅包含数组中的单个元素,即最大元素。
  3. 当K = 2时,这是最棘手的情况。因为只有两个子数组,所以只会有一个前缀和后缀。维护一组前缀最小值和后缀最小值。然后,对于每个元素arr [i] ,更新ans = max(ans,max(在i处前缀最小值,在i +1后缀最小值))

下面是上述方法的实现:

C++
// C++ implementation of the above approach
  
#include 
using namespace std;
  
// Function to return maximum possible value
// of maximum of minimum of K sub-arrays
int maximizeMinimumOfKSubarrays(const int* arr, int n, int k)
{
    int m = INT_MAX;
    int M = INT_MIN;
  
    // Compute maximum and minimum
    // of the array
    for (int i = 0; i < n; i++) {
        m = min(m, arr[i]);
        M = max(M, arr[i]);
    }
  
    // If k = 1 then return the
    // minimum of the array
    if (k == 1) {
        return m;
    }
    // If k >= 3 then return the
    // maximum of the array
    else if (k >= 3) {
        return M;
    }
  
    // If k = 2 then maintain prefix
    // and suffix minimums
    else {
  
        // Arrays to store prefix
        // and suffix minimums
        int L[n], R[n];
  
        L[0] = arr[0];
        R[n - 1] = arr[n - 1];
  
        // Prefix minimum
        for (int i = 1; i < n; i++)
            L[i] = min(L[i - 1], arr[i]);
  
        // Suffix minimum
        for (int i = n - 2; i >= 0; i--)
            R[i] = min(R[i + 1], arr[i]);
  
        int maxVal = INT_MIN;
  
        // Get the maximum possible value
        for (int i = 0; i < n - 1; i++)
            maxVal = max(maxVal, max(L[i], R[i + 1]));
  
        return maxVal;
    }
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 2;
  
    cout << maximizeMinimumOfKSubarrays(arr, n, k);
  
    return 0;
}


Java
// Java implementation of the above approach
class GFG {
  
    // Function to return maximum possible value
    // of maximum of minimum of K sub-arrays
    static int maximizeMinimumOfKSubarrays(int arr[], int n, int k)
    {
        int m = Integer.MAX_VALUE;
        int M = Integer.MIN_VALUE;
  
        // Compute maximum and minimum
        // of the array
        for (int i = 0; i < n; i++) {
            m = Math.min(m, arr[i]);
            M = Math.max(M, arr[i]);
        }
  
        // If k = 1 then return the
        // minimum of the array
        if (k == 1) {
            return m;
        }
  
        // If k >= 3 then return the
        // maximum of the array
        else if (k >= 3) {
            return M;
        }
  
        // If k = 2 then maintain prefix
        // and suffix minimums
        else {
  
            // Arrays to store prefix
            // and suffix minimums
            int L[] = new int[n], R[] = new int[n];
  
            L[0] = arr[0];
            R[n - 1] = arr[n - 1];
  
            // Prefix minimum
            for (int i = 1; i < n; i++)
                L[i] = Math.min(L[i - 1], arr[i]);
  
            // Suffix minimum
            for (int i = n - 2; i >= 0; i--)
                R[i] = Math.min(R[i + 1], arr[i]);
  
            int maxVal = Integer.MIN_VALUE;
  
            // Get the maximum possible value
            for (int i = 0; i < n - 1; i++)
                maxVal = Math.max(maxVal, Math.max(L[i], R[i + 1]));
  
            return maxVal;
        }
    }
  
    // Driver code
    public static void main(String args[])
    {
        int arr[] = { 1, 2, 3, 4, 5 };
        int n = arr.length;
        int k = 2;
  
        System.out.println(maximizeMinimumOfKSubarrays(arr, n, k));
    }
}
  
// This code is contributed by Arnab Kundu


Python3
# Python3 implementation of the above approach 
import sys
  
# Function to return maximum possible value 
# of maximum of minimum of K sub-arrays 
def maximizeMinimumOfKSubarrays(arr, n, k) :
      
    m = sys.maxsize; 
    M = -(sys.maxsize - 1); 
  
    # Compute maximum and minimum 
    # of the array 
    for i in range(n) :
        m = min(m, arr[i]); 
        M = max(M, arr[i]); 
  
    # If k = 1 then return the 
    # minimum of the array 
    if (k == 1) :
        return m;
          
    # If k >= 3 then return the 
    # maximum of the array 
    elif (k >= 3) :
        return M;
  
    # If k = 2 then maintain prefix 
    # and suffix minimums 
    else :
          
        # Arrays to store prefix 
        # and suffix minimums
        L = [0] * n;
        R = [0] * n;
          
        L[0] = arr[0];
        R[n - 1] = arr[n - 1]; 
          
        # Prefix minimum
        for i in range(1, n) :
            L[i] = min(L[i - 1], arr[i]); 
          
        # Suffix minimum
        for i in range(n - 2, -1, -1) :
            R[i] = min(R[i + 1], arr[i]); 
          
        maxVal = -(sys.maxsize - 1); 
          
        # Get the maximum possible value
        for i in range(n - 1) :
            maxVal = max(maxVal, max(L[i], 
                                 R[i + 1])); 
          
        return maxVal; 
  
# Driver code 
if __name__ == "__main__" : 
  
    arr = [ 1, 2, 3, 4, 5 ]; 
    n = len(arr);
    k = 2; 
  
    print(maximizeMinimumOfKSubarrays(arr, n, k));
      
# This code is contributed by Ryuga


C#
// C# implemenatation of above approach
using System;
  
public class GFG {
  
    // Function to return maximum possible value
    // of maximum of minimum of K sub-arrays
    static int maximizeMinimumOfKSubarrays(int[] arr, int n, int k)
    {
        int m = int.MaxValue;
        int M = int.MinValue;
  
        // Compute maximum and minimum
        // of the array
        for (int i = 0; i < n; i++) {
            m = Math.Min(m, arr[i]);
            M = Math.Max(M, arr[i]);
        }
  
        // If k = 1 then return the
        // minimum of the array
        if (k == 1) {
            return m;
        }
  
        // If k >= 3 then return the
        // maximum of the array
        else if (k >= 3) {
            return M;
        }
  
        // If k = 2 then maintain prefix
        // and suffix minimums
        else {
  
            // Arrays to store prefix
            // and suffix minimums
            int[] L = new int[n];
            int[] R = new int[n];
  
            L[0] = arr[0];
            R[n - 1] = arr[n - 1];
  
            // Prefix minimum
            for (int i = 1; i < n; i++)
                L[i] = Math.Min(L[i - 1], arr[i]);
  
            // Suffix minimum
            for (int i = n - 2; i >= 0; i--)
                R[i] = Math.Min(R[i + 1], arr[i]);
  
            int maxVal = int.MinValue;
  
            // Get the maximum possible value
            for (int i = 0; i < n - 1; i++)
                maxVal = Math.Max(maxVal, Math.Max(L[i], R[i + 1]));
  
            return maxVal;
        }
    }
  
    // Driver code
    public static void Main()
    {
        int[] arr = { 1, 2, 3, 4, 5 };
        int n = arr.Length;
        int k = 2;
  
        Console.WriteLine(maximizeMinimumOfKSubarrays(arr, n, k));
    }
}
/* This code contributed by PrinciRaj1992 */


PHP
= 3 then return the
    // maximum of the array
    else if ($k >= 3) 
    {
        return $M;
    }
  
    // If k = 2 then maintain prefix
    // and suffix minimums
    else
    {
  
        // Arrays to store prefix
        // and suffix minimums
        $L[0] = $arr[0];
        $R[$n - 1] = $arr[$n - 1];
  
        // Prefix minimum
        for ($i = 1; $i < $n; $i++)
            $L[$i] = min($L[$i - 1], $arr[$i]);
  
        // Suffix minimum
        for ($i = $n - 2; $i >= 0; $i--)
            $R[$i] = min($R[$i + 1], $arr[$i]);
  
        $maxVal = PHP_INT_MIN;
  
        // Get the maximum possible value
        for ($i = 0; $i < $n - 1; $i++)
            $maxVal = max($maxVal, 
                      max($L[$i], $R[$i + 1]));
  
        return $maxVal;
    }
}
  
// Driver code
$arr = array( 1, 2, 3, 4, 5 );
$n = sizeof($arr);
$k = 2;
  
echo maximizeMinimumOfKSubarrays($arr, $n, $k);
  
// This code is contributed 
// by Akanksha Rai
?>


输出:
5