📌  相关文章
📜  通过将Array元素替换为Square可以得到的最大子数组总和

📅  最后修改于: 2021-05-17 16:17:52             🧑  作者: Mango

给定一个由N个整数组成的数组a [] ,任务是找到可以通过将单个数组元素替换为其平方而获得的最大子数组和。

例子:

天真的方法:解决问题的最简单方法是将每个元素替换为其正方形,并针对每个元素,使用Kadane算法找到最大子数组总和。最后,打印获得的最大可能子数组总和。

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

高效方法:可以使用动态编程来优化上述方法请按照以下步骤解决问题:

  • 初始化存储表dp [] [],其中:
  • dp [i] [0]:存储可以获得的最大子数组和,包括i元素,并且不对任何数组元素求平方。
  • dp [i] [1]:存储可以包含i元素且不对数组元素进行平方的最大子数组和
  • 因此,递归关系为:

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include   
using namespace std;  
  
// Function to find the maximum subarray
// sum possible
int getMaxSum(int a[], int n)
{
    int dp[n][2];
  
    // Stores sum without squaring
    dp[0][0] = a[0];
  
    // Stores sum squaring
    dp[0][1] = a[0] * a[0];
  
    // Stores the maximum subarray sum
    int max_sum = max(dp[0][0], dp[0][1]);
    for(int i = 1; i < n; i++)
    {
          
        // Either extend the subarray
        // or start a new subarray
        dp[i][0] = max(a[i],
                      dp[i - 1][0] + a[i]);
  
        // Either extend previous squared
        // subarray or start a new subarray
        // by squaring the current element
        dp[i][1] = max(dp[i - 1][1] + a[i],
                               a[i] * a[i]);
  
        dp[i][1] = max(dp[i][1],
                       dp[i - 1][0] + 
                       a[i] * a[i]);
  
        // Update maximum subarray sum
        max_sum = max(max_sum, dp[i][1]);
        max_sum = max(max_sum, dp[i][0]);
    }
      
    // Return answer
    return max_sum;
}
      
// Driver Code
int32_t main()
{
    int n = 5;
    int a[] = { 1, -5, 8, 12, -8 };
  
    // Function call
    cout << getMaxSum(a, n) << endl;
  
    return 0;
} 
  
// This code is contributed by rutvik_56


Java
// Java Program to implement 
// the above approach 
import java.io.*; 
  
class GFG { 
  
    // Function to find the maximum subarray 
    // sum possible 
    public static int getMaxSum(int a[], int n) 
    { 
        int dp[][] = new int[n][2]; 
  
        // Stores sum without squaring 
        dp[0][0] = a[0]; 
  
        // Stores sum squaring 
        dp[0][1] = a[0] * a[0]; 
  
        // Stores the maximum subarray sum 
        int max_sum = Math.max(dp[0][0], dp[0][1]); 
        for (int i = 1; i < n; i++) { 
  
            // Either extend the subarray 
            // or start a new subarray 
            dp[i][0] = Math.max(a[i], 
                                dp[i - 1][0] + a[i]); 
  
            // Either extend previous squared 
            // subarray or start a new subarray 
            // by squaring the current element 
            dp[i][1] = Math.max(dp[i - 1][1] + a[i], 
                                a[i] * a[i]); 
  
            dp[i][1] 
                = Math.max(dp[i][1], 
                        dp[i - 1][0] + a[i] * a[i]); 
  
            // Update maximum subarray sum 
            max_sum = Math.max(max_sum, dp[i][1]); 
            max_sum = Math.max(max_sum, dp[i][0]); 
        } 
  
        // Return answer 
        return max_sum; 
    } 
  
    // Driver Code 
    public static void main(String[] args) 
    { 
        int n = 5; 
        int a[] = { 1, -5, 8, 12, -8 }; 
  
        // Function call 
        System.out.println(getMaxSum(a, n)); 
    } 
}


Python3
# Python3 program to implement 
# the above approach 
  
# Function to find the maximum subarray 
# sum possible 
def getMaxSum(a, n): 
  
    dp = [[0 for x in range(2)] 
            for y in range(n)] 
  
    # Stores sum without squaring 
    dp[0][0] = a[0] 
  
    # Stores sum squaring 
    dp[0][1] = a[0] * a[0] 
  
    # Stores the maximum subarray sum 
    max_sum = max(dp[0][0], dp[0][1]) 
  
    for i in range(1, n): 
  
        # Either extend the subarray 
        # or start a new subarray 
        dp[i][0] = max(a[i], 
                    dp[i - 1][0] + a[i]) 
  
        # Either extend previous squared 
        # subarray or start a new subarray 
        # by squaring the current element 
        dp[i][1] = max(dp[i - 1][1] + a[i], 
                        a[i] * a[i]) 
  
        dp[i][1] = max(dp[i][1], 
                    dp[i - 1][0] +
                        a[i] * a[i]) 
  
        # Update maximum subarray sum 
        max_sum = max(max_sum, dp[i][1]) 
        max_sum = max(max_sum, dp[i][0]) 
  
    # Return answer 
    return max_sum 
  
# Driver Code 
n = 5
a = [ 1, -5, 8, 12, -8 ] 
  
# Function call 
print(getMaxSum(a, n)) 
  
# This code is contributed by Shivam Singh


C#
// C# program to implement 
// the above approach 
using System; 
  
class GFG{ 
  
// Function to find the maximum subarray 
// sum possible 
public static int getMaxSum(int []a, int n) 
{ 
    int [,]dp = new int[n, 2]; 
  
    // Stores sum without squaring 
    dp[0, 0] = a[0]; 
  
    // Stores sum squaring 
    dp[0, 1] = a[0] * a[0]; 
  
    // Stores the maximum subarray sum 
    int max_sum = Math.Max(dp[0, 0], dp[0, 1]); 
    for(int i = 1; i < n; i++) 
    { 
          
        // Either extend the subarray 
        // or start a new subarray 
        dp[i, 0] = Math.Max(a[i], 
                        dp[i - 1, 0] + a[i]); 
  
        // Either extend previous squared 
        // subarray or start a new subarray 
        // by squaring the current element 
        dp[i, 1] = Math.Max(dp[i - 1, 1] + a[i], 
                            a[i] * a[i]); 
  
        dp[i, 1] = Math.Max(dp[i, 1], 
                            dp[i - 1, 0] + 
                            a[i] * a[i]); 
  
        // Update maximum subarray sum 
        max_sum = Math.Max(max_sum, dp[i, 1]); 
        max_sum = Math.Max(max_sum, dp[i, 0]); 
    } 
  
    // Return answer 
    return max_sum; 
} 
  
// Driver Code 
public static void Main(String[] args) 
{ 
    int n = 5; 
    int []a = { 1, -5, 8, 12, -8 }; 
  
    // Function call 
    Console.WriteLine(getMaxSum(a, n)); 
} 
} 
  
// This code is contributed by PrinciRaj1992


输出:
152

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