📌  相关文章
📜  生成一个最大和的数组,使得每个元素超过其左侧或右侧存在的所有元素

📅  最后修改于: 2021-10-26 06:33:38             🧑  作者: Mango

给定一个由N 个正整数组成的数组A[] ,任务是构造一个大小为N的数组B[] ,该数组具有最大可能的数组元素总和,对于每个索引i满足以下条件:

  • 数组元素B[i]必须小于或等于A[i]
  • 对于每个索引iB[i]必须大于其左侧或右侧的所有元素。

例子:

方法:给定的问题可以通过观察这样一个事实来解决数组中总是有一个最大的元素,它首先增加然后单调减少。因此,思路是让新数组B[]的每一个数组元素一个一个最大,然后检查最大和。请按照以下步骤解决给定的问题:

  • 初始化数组,比如分别存储数组元素A[]和结果数组的arrA[]ans[]
  • 初始化一个变量,比如maxSum0 ,它存储数组元素的最大总和。
  • 迭代范围[0, N]并执行以下步骤:
    • 初始化一个数组,比如可以存储结果数组的arrB[]
    • 以单调递增的顺序分配所有数组元素arrB[i]直到每个索引i
    • 在范围[i, N]内以单调递减的顺序分配所有数组元素arrB[i ]
    • 现在,找到数组arrB[]的总和,如果总和大于maxSum,则将maxSum和数组ans[]更新为当前总和和构造的当前数组arrB[]
  • 完成上述步骤后,打印数组arrB[]作为结果数组。

下面是上述方法的实现:

C++
// C++ code for the above appproach
#include 
using namespace std;
 
// Function to construct the array
// having maximum sum satisfying the
// given criteria
void maximumSumArray(int arr[], int N)
{
    // Declaration of the array arrA[]
    // and ans[]
    vector arrA(N), ans(N);
 
    // Stores the maximum sum of the
    // resultant array
    int maxSum = 0;
 
    // Initialize the array arrA[]
    for (int i = 0; i < N; i++)
        arrA[i] = arr[i];
 
    // Traversing the array arrA[]
    for (int i = 0; i < N; i++) {
 
        // Initialize the array arrB[]
        vector arrB(N);
        int maximum = arrA[i];
 
        // Assign the maximum element
        // to the current element
        arrB[i] = maximum;
 
        // Form the first increasing
        // till every index i
        for (int j = i - 1; j >= 0; j--) {
            arrB[j] = min(maximum, arrA[j]);
            maximum = arrB[j];
        }
 
        // Make the current element
        // as the maximum element
        maximum = arrA[i];
 
        // Forming decreasing from the
        // index i + 1 to the index N
        for (int j = i + 1; j < N; j++) {
            arrB[j] = min(maximum, arrA[j]);
            maximum = arrB[j];
        }
 
        // Initialize the sum
        int sum = 0;
 
        // Find the total sum
        for (int j = 0; j < N; j++)
            sum += arrB[j];
 
        // Check if the total sum is
        // at least the sum found
        // then make ans as ansB
        if (sum > maxSum) {
            maxSum = sum;
            ans = arrB;
        }
    }
 
    // Print the final array formed
    for (int val : ans) {
        cout << val << " ";
    }
}
 
// Driver Code
int main()
{
    int A[] = { 10, 6, 8 };
    int N = sizeof(A) / sizeof(A[0]);
    maximumSumArray(A, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG {
 
// Function to construct the array
// having maximum sum satisfying the
// given criteria
static void maximumSumArray(int arr[], int N)
{
    // Declaration of the array arrA[]
    // and ans[]
    int[] arrA  = new int[(N)];
    int[] ans  = new int[(N)];
 
    // Stores the maximum sum of the
    // resultant array
    int maxSum = 0;
 
    // Initialize the array arrA[]
    for (int i = 0; i < N; i++)
        arrA[i] = arr[i];
 
    // Traversing the array arrA[]
    for (int i = 0; i < N; i++) {
 
        // Initialize the array arrB[]
        int[] arrB = new int[(N)];
        int maximum = arrA[i];
 
        // Assign the maximum element
        // to the current element
        arrB[i] = maximum;
 
        // Form the first increasing
        // till every index i
        for (int j = i - 1; j >= 0; j--) {
            arrB[j] = Math.min(maximum, arrA[j]);
            maximum = arrB[j];
        }
 
        // Make the current element
        // as the maximum element
        maximum = arrA[i];
 
        // Forming decreasing from the
        // index i + 1 to the index N
        for (int j = i + 1; j < N; j++) {
            arrB[j] = Math.min(maximum, arrA[j]);
            maximum = arrB[j];
        }
 
        // Initialize the sum
        int sum = 0;
 
        // Find the total sum
        for (int j = 0; j < N; j++)
            sum += arrB[j];
 
        // Check if the total sum is
        // at least the sum found
        // then make ans as ansB
        if (sum > maxSum) {
            maxSum = sum;
            ans = arrB;
        }
    }
 
    // Print the final array formed
    for (int val : ans) {
        System.out.print(val + " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int A[] = { 10, 6, 8 };
    int N = A.length;
    maximumSumArray(A, N);
}
}
 
// This code is contributed by splevel62.


Python3
# Python program for the above approach;
 
# Function to construct the array
# having maximum sum satisfying the
# given criteria
def maximumSumArray(arr, N):
     
    # Declaration of the array arrA[]
    # and ans[]
    arrA = [0] * N
    ans = [0] * N
 
    # Stores the maximum sum of the
    # resultant array
    maxSum = 0;
 
    # Initialize the array arrA[]
    for i in range(N):
        arrA[i] = arr[i];
 
    # Traversing the array arrA[]
    for i in range(N):
         
        # Initialize the array arrB[]
        arrB = [0] * N
        maximum = arrA[i];
 
        # Assign the maximum element
        # to the current element
        arrB[i] = maximum;
 
        temp = 0
         
        # Form the first increasing
        # till every index i
        for j in range(i - 1, -1, -1):
            arrB[j] = min(maximum, arrA[j]);
            maximum = arrB[j];
            temp = j
         
 
        # Make the current element
        # as the maximum element
        maximum = arrA[i];
 
        # Forming decreasing from the
        # index i + 1 to the index N
        for j in range(i + 1, N):
            arrB[j] = min(maximum, arrA[j]);
            maximum = arrB[j];
         
 
        # Initialize the sum
        sum = 0;
 
        # Find the total sum
        for j in range(N):
            sum += arrB[j];
 
        # Check if the total sum is
        # at least the sum found
        # then make ans as ansB
        if (sum > maxSum):
            maxSum = sum;
            ans = arrB;
         
    # Print the final array formed
    for val in ans:
        print(val);
     
# Driver Code
A = [ 10, 6, 8 ];
N = len(A)
 
maximumSumArray(A, N);
 
# This code is contributed by _Saurabh_Jaiswal


C#
// C# code for the above appproach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to construct the array
// having maximum sum satisfying the
// given criteria
static void maximumSumArray(int []arr, int N)
{
    // Declaration of the array arrA[]
    // and ans[]
    int []arrA = new int[N];
    int []ans = new int[N];
 
    // Stores the maximum sum of the
    // resultant array
    int maxSum = 0;
 
    // Initialize the array arrA[]
    for (int i = 0; i < N; i++)
        arrA[i] = arr[i];
 
    // Traversing the array arrA[]
    for (int i = 0; i < N; i++) {
 
        // Initialize the array arrB[]
        int []arrB = new int[N];
        int maximum = arrA[i];
 
        // Assign the maximum element
        // to the current element
        arrB[i] = maximum;
 
        // Form the first increasing
        // till every index i
        for (int j = i - 1; j >= 0; j--) {
            arrB[j] = Math.Min(maximum, arrA[j]);
            maximum = arrB[j];
        }
 
        // Make the current element
        // as the maximum element
        maximum = arrA[i];
 
        // Forming decreasing from the
        // index i + 1 to the index N
        for (int j = i + 1; j < N; j++) {
            arrB[j] = Math.Min(maximum, arrA[j]);
            maximum = arrB[j];
        }
 
        // Initialize the sum
        int sum = 0;
 
        // Find the total sum
        for (int j = 0; j < N; j++)
            sum += arrB[j];
 
        // Check if the total sum is
        // at least the sum found
        // then make ans as ansB
        if (sum > maxSum) {
            maxSum = sum;
            ans = arrB;
        }
    }
 
    // Print the final array formed
    foreach (int val in ans) {
        Console.Write(val + " ");
    }
}
 
// Driver Code
public static void Main()
{
    int []A = { 10, 6, 8 };
    int N = A.Length;
    maximumSumArray(A, N);
}
}
 
// This code is contributed by SURENDRA_GANGWAR.


Javascript


输出:
10 6 6

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