📌  相关文章
📜  通过恰好 K 减量最大化可能的最小数组元素

📅  最后修改于: 2022-05-13 01:56:07.681000             🧑  作者: Mango

通过恰好 K 减量最大化可能的最小数组元素

给定一个由N个整数和一个整数K组成的数组arr[] ,任务是在将数组元素恰好递减K次后最大化数组的最小元素。

例子:

朴素方法:解决给定问题的最简单方法是迭代范围[1, K]并在每次迭代中找到数组的最大元素,然后将其递减1 。在上述步骤之后,然后打印数组的最小元素。

时间复杂度: O(N * K)
辅助空间: O(1)

有效方法:上述问题也可以根据以下观察进行优化:

  • 可以观察到,最优的方式是先将数组的所有值递减到数组的最小元素。
  • 使所有元素等于最小元素所需的总移动是数组的总和减去K乘以数组的最小元素。
  • 如果使所有元素等于最小元素所需的移动总数小于K ,则最小元素将是答案。
  • 否则,最好从数组的每个元素递减1直到K变为0 。那么最小元素将等于minElement - ceil( K / N) = minElement - (K + N - 1) / N         .

请按照以下步骤解决问题:

  • 找到数组arr[]的最小元素并将其存储在一个变量中,比如minElement
  • 初始化一个变量,比如reqOperation0以存储使所有元素等于数组的minElement所需的移动总数。
  • 遍历数组arr[]并在每次迭代中,将reqOperation增加数组的当前元素减去minElement
  • 如果reqOperation大于K则打印minElement 。否则,打印minElement – (K + N – 1) / N的值作为结果最小元素。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximized
// minimum element of the array
// after performing given operation
// exactly K times
int minimumElement(int arr[], int N,
                   int K)
{
    // Stores the minimum element
    int minElement = arr[0];
 
    // Traverse the given array
    for (int i = 0; i < N; ++i) {
 
        // Update the minimum element
        minElement = min(minElement,
                         arr[i]);
    }
 
    // Stores the required operations
    // to make all elements equal to
    // the minimum element
    int reqOperations = 0;
 
    for (int i = 0; i < N; ++i) {
 
        // Update required operations
        reqOperations += arr[i] - minElement;
    }
 
    // If reqOperations < K
    if (reqOperations < K) {
 
        // Decrement the value of K
        // by reqOperations
        K -= reqOperations;
 
        // Update minElement
        minElement -= (K + N - 1) / N;
    }
 
    // Return minimum element
    return minElement;
}
 
// Driver Code
int main()
{
 
    int arr[] = { 10, 10, 10, 10 };
    int K = 7;
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << minimumElement(arr, N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG
{
   
    // Function to find the maximized
    // minimum element of the array
    // after performing given operation
    // exactly K times
    static int minimumElement(int arr[], int N, int K)
    {
       
        // Stores the minimum element
        int minElement = arr[0];
 
        // Traverse the given array
        for (int i = 0; i < N; ++i) {
 
            // Update the minimum element
            minElement = Math.min(minElement, arr[i]);
        }
 
        // Stores the required operations
        // to make all elements equal to
        // the minimum element
        int reqOperations = 0;
 
        for (int i = 0; i < N; ++i) {
 
            // Update required operations
            reqOperations += arr[i] - minElement;
        }
 
        // If reqOperations < K
        if (reqOperations < K) {
 
            // Decrement the value of K
            // by reqOperations
            K -= reqOperations;
 
            // Update minElement
            minElement -= (K + N - 1) / N;
        }
 
        // Return minimum element
        return minElement;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 10, 10, 10, 10 };
        int K = 7;
        int N = arr.length;
        System.out.println(minimumElement(arr, N, K));
 
    }
}
 
        // This code is contributed by Potta Lokesh


Python3
# Python program for the above approach
 
# Function to find the maximized
# minimum element of the array
# after performing given operation
# exactly K times
def minimumElement(arr, N, K):
     
    # Stores the minimum element
    minElement = arr[0];
 
    # Traverse the given array
    for i in range(N):
         
        # Update the minimum element
        minElement = min(minElement, arr[i]);
 
    # Stores the required operations
    # to make all elements equal to
    # the minimum element
    reqOperations = 0;
 
    for i in range(N):
         
        # Update required operations
        reqOperations += arr[i] - minElement
 
    # If reqOperations < K
    if (reqOperations < K):
         
        # Decrement the value of K
        # by reqOperations
        K -= reqOperations;
 
        # Update minElement
        minElement -= (K + N - 1) // N;
 
    # Return minimum element
    return minElement;
 
 
# Driver Code
arr = [ 10, 10, 10, 10 ];
K = 7;
N = len(arr)
 
print(minimumElement(arr, N, K));
 
# This code is contributed by _saurabh_jaiswal


C#
// C# program for the above approach
using System;
class GFG
{
   
    // Function to find the maximized
    // minimum element of the array
    // after performing given operation
    // exactly K times
    static int minimumElement(int []arr, int N, int K)
    {
       
        // Stores the minimum element
        int minElement = arr[0];
 
        // Traverse the given array
        for (int i = 0; i < N; ++i) {
 
            // Update the minimum element
            minElement = Math.Min(minElement, arr[i]);
        }
 
        // Stores the required operations
        // to make all elements equal to
        // the minimum element
        int reqOperations = 0;
 
        for (int i = 0; i < N; ++i) {
 
            // Update required operations
            reqOperations += arr[i] - minElement;
        }
 
        // If reqOperations < K
        if (reqOperations < K) {
 
            // Decrement the value of K
            // by reqOperations
            K -= reqOperations;
 
            // Update minElement
            minElement -= (K + N - 1) / N;
        }
 
        // Return minimum element
        return minElement;
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int[] arr= { 10, 10, 10, 10 };
        int K = 7;
        int N = arr.Length;
        Console.Write(minimumElement(arr, N, K));
 
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
8

时间复杂度: O(N)
辅助空间: O(1)