📌  相关文章
📜  通过大小为S的M个子数组增量最大化最小数组元素

📅  最后修改于: 2021-04-22 00:19:35             🧑  作者: Mango

给定N个整数和两个整数SM的数组arr [] ,任务是通过将大小为S的任何子数组增加1到M次来最大化最小数组元素。

例子:

方法:这个想法是找到数组M的最小元素次,然后从该最小元素开始将大小为S的子数组增加1 。请按照以下步骤解决问题:

  • 遍历数组M次,对于每次迭代,请执行以下操作:
    • 找到数组arr []的最小元素。令最小元素的第一个索引为idx
    • 将当前的最小元素增加1
    • 现在将两个指针leftIdx作为idx – 1rightIdx作为idx + 1
    • 如果leftIdx处的元素小于rightIdx处的元素,则将A [leftIndex]递增1,并将leftIndex递减1 。否则,将A [rightIndex]增大1并将rightIndex增大1 。继续此步骤,直到处理(S – 1)个元素。
  • 在上述迭代之后,打印更新后的数组的最小元素。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to return index of minimum
// element in the array
int min(int a[], int n)
{
    // Initialize a[0] as minValue
    int minIndex = 0, minValue = a[0], i;
 
    // Traverse the array
    for (i = 1; i < n; i++) {
 
        // If a[i] < existing minValue
        if (a[i] < minValue) {
            minValue = a[i];
            minIndex = i;
        }
    }
 
    // Return the minimum index
    return minIndex;
}
 
// Function that maximize the minimum
// element of array after incrementing
// subarray of size S by 1, M times
int maximizeMin(int A[], int N,
                int S, int M)
{
    int minIndex, left, right, i, j;
 
    // Iterating through the array
    // for M times
    for (i = 0; i < M; i++) {
 
        // Find minimum element index
        minIndex = min(A, N);
 
        // Increment the minimum value
        A[minIndex]++;
 
        // Storing the left index
        // and right index
        left = minIndex - 1;
        right = minIndex + 1;
 
        // Incrementing S - 1 minumum
        // elements to the left and
        // right of minValue
        for (j = 0; j < S - 1; j++) {
 
            // Reached extreme left
            if (left == -1)
                A[right++]++;
 
            // Reached extreme right
            else if (right == N)
                A[left--]++;
 
            else {
 
                // Left value is minimum
                if (A[left] < A[right])
                    A[left--]++;
 
                // Right value is minimum
                else
                    A[right++]++;
            }
        }
    }
 
    // Find the minValue in A[] after
    // M operations
    minIndex = min(A, N);
 
    // Return the minimum value
    return A[minIndex];
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int S = 2, M = 3;
 
    // Function Call
    cout << maximizeMin(arr, N, S, M);
 
    return 0;
}


Java
// Java program for the
// above approach
import java.util.*;
class solution{
 
// Function to return index
// of minimum element in the
// array
static int min1(int a[], int n)
{
  // Initialize a[0] as
  // minValue
  int minIndex = 0,
      minValue = a[0], i;
 
  // Traverse the array
  for (i = 1; i < n; i++)
  {
    // If a[i] < existing
    // minValue
    if (a[i] < minValue)
    {
      minValue = a[i];
      minIndex = i;
    }
  }
 
  // Return the minimum index
  return minIndex;
}
 
// Function that maximize the minimum
// element of array after incrementing
// subarray of size S by 1, M times
static int maximizeMin(int A[], int N,
                       int S, int M)
{
  int minIndex, left, right, i, j;
 
  // Iterating through the
  // array or M times
  for (i = 0; i < M; i++)
  {
    // Find minimum element
    // index
    minIndex = min1(A, N);
 
    // Increment the minimum
    // value
    A[minIndex]++;
 
    // Storing the left index
    // and right index
    left = minIndex - 1;
    right = minIndex + 1;
 
    // Incrementing S - 1 minumum
    // elements to the left and
    // right of minValue
    for (j = 0; j < S - 1; j++)
    {
      // Reached extreme left
      if (left == -1)
        A[right++]++;
 
      // Reached extreme right
      else if (right == N)
        A[left--]++;
 
      else
      {
        // Left value is minimum
        if (A[left] < A[right])
          A[left--]++;
 
        // Right value is minimum
        else
          A[right++]++;
      }
    }
  }
 
  // Find the minValue in A[] after
  // M operations
  minIndex = min1(A, N);
 
  // Return the minimum value
  return A[minIndex];
}
 
// Driver Code
public static void main(String args[])
{
  int []arr = {1, 2, 3,
               4, 5, 6};
  int N = arr.length;
  int S = 2, M = 3;
 
  // Function Call
  System.out.print(maximizeMin(arr, N, S, M));
}
}
 
// This code is contributed by SURENDRA_GANGWAR


Python3
# Python3 program for the above approach
 
# Function to return index of minimum
# element in the array
def min(a, n):
     
    # Initialize a[0] as minValue
    minIndex = 0
    minValue = a[0]
 
    # Traverse the array
    for i in range(1, n):
         
        # If a[i] < existing minValue
        if (a[i] < minValue):
            minValue = a[i]
            minIndex = i
 
    # Return the minimum index
    return minIndex
 
# Function that maximize the minimum
# element of array after incrementing
# subarray of size S by 1, M times
def maximizeMin(A, N, S, M):
     
    minIndex, left, right = 0, 0, 0
     
    # Iterating through the array
    # for M times
    for i in range(M):
         
        # Find minimum element index
        minIndex = min(A, N)
 
        # Increment the minimum value
        A[minIndex] += 1
 
        # Storing the left index
        # and right index
        left = minIndex - 1
        right = minIndex + 1
 
        # Incrementing S - 1 minumum
        # elements to the left and
        # right of minValue
        for j in range(S - 1):
             
            # Reached extreme left
            if (left == -1):
                A[right] += 1
                right += 1
                 
            # Reached extreme right
            elif (right == N):
                A[left] += 1
                left -= 1
 
            else:
 
                # Left value is minimum
                if (A[left] < A[right]):
                    A[left] += 1
                    left -= 1
                     
                # Right value is minimum
                else:
                    A[right] += 1
                    right += 1
 
    # Find the minValue in A[] after
    # M operations
    minIndex = min(A, N)
 
    # Return the minimum value
    return A[minIndex]
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 2, 3, 4, 5, 6 ]
    N = len(arr)
    S = 2
    M = 3
     
    #Function Call
    print(maximizeMin(arr, N, S, M))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the
// above approach
using System;
class GFG{
     
// Function to return index
// of minimum element in the
// array
static int min1(int[] a,
                int n)
{
  // Initialize a[0] as
  // minValue
  int minIndex = 0,
      minValue = a[0], i;
 
  // Traverse the array
  for (i = 1; i < n; i++)
  {
    // If a[i] < existing
    // minValue
    if (a[i] < minValue)
    {
      minValue = a[i];
      minIndex = i;
    }
  }
 
  // Return the minimum
  // index
  return minIndex;
}
      
// Function that maximize the
// minimum element of array
// after incrementing subarray
// of size S by 1, M times
static int maximizeMin(int[] A, int N,
                       int S, int M)
{
  int minIndex, left, right, i, j;
 
  // Iterating through the
  // array or M times
  for (i = 0; i < M; i++)
  {
    // Find minimum element
    // index
    minIndex = min1(A, N);
 
    // Increment the minimum
    // value
    A[minIndex]++;
 
    // Storing the left index
    // and right index
    left = minIndex - 1;
    right = minIndex + 1;
 
    // Incrementing S - 1 minumum
    // elements to the left and
    // right of minValue
    for (j = 0; j < S - 1; j++)
    {
      // Reached extreme left
      if (left == -1)
        A[right++]++;
 
      // Reached extreme right
      else if (right == N)
        A[left--]++;
 
      else
      {
        // Left value is minimum
        if (A[left] < A[right])
          A[left--]++;
 
        // Right value is minimum
        else
          A[right++]++;
      }
    }
  }
 
  // Find the minValue in A[] after
  // M operations
  minIndex = min1(A, N);
 
  // Return the minimum value
  return A[minIndex];
}
   
// Driver code
static void Main()
{
  int[] arr = {1, 2, 3,
               4, 5, 6};
  int N = arr.Length;
  int S = 2, M = 3;
 
  // Function Call
  Console.Write(maximizeMin(arr, N,
                            S, M));
}
}
 
// This code is contributed by divyeshrabadiya07


输出:
3

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