📜  在元素上交替放置加减号后最大化子序列和

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

在元素上交替放置加减号后最大化子序列和

给定一个大小为N的数组arr[] 。任务是找到通过替代减法可以达到的最大分数 - 加数组中任何子序列的元素。

例子:

方法:我们可以使用动态规划方法来解决这个问题。

  • 假设dp1 i ⇢ 是前i个元素的前缀上的子序列的最大可能总和,前提是子序列的长度是奇数。同样输入dp2 i ⇢ 仅用于偶数长度的子序列。
  • 然后dp1 idp2 i很容易重新计算为:
    • dp1 i+1 = max(dp1 i , dp2 i +arr i )
    • dp2 i+1 = max(dp2 i , dp1 i −arr i )
  • 初始值为dp1 0 = -∞ , dp2 0 = 0并且答案将存储在max ( dp1 n , dp2 n ) 中。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
#define INF 1e9
 
// Function to find the maximum score
// achieved by alternative minus-plus
// of elements of a subsequence in
// the given array
int maxScore(int arr[], int N)
{
    vector dp1(N + 1);
    vector dp2(N + 1);
 
    dp1[0] = -INF;
    dp2[0] = 0;
 
    for (int i = 0; i < N; ++i) {
        dp1[i + 1] = max(dp1[i], dp2[i] + arr[i]);
        dp2[i + 1] = max(dp2[i], dp1[i] - arr[i]);
    }
    // Return the maximum
    return max(dp1.back(), dp2.back());
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 3, 6, 5, 4, 7, 8 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << maxScore(arr, N);
    return 0;
}


Java
// Java program for the above approach
class GFG {
 
    static double INF = 1E9;
 
    // Function to find the maximum score
    // achieved by alternative minus-plus
    // of elements of a subsequence in
    // the given array
    public static int maxScore(int arr[], int N) {
        int[] dp1 = new int[N + 1];
        int[] dp2 = new int[N + 1];
 
        dp1[0] = (int) -INF;
        dp2[0] = 0;
 
        for (int i = 0; i < N; ++i) {
            dp1[i + 1] = Math.max(dp1[i], dp2[i] + arr[i]);
            dp2[i + 1] = Math.max(dp2[i], dp1[i] - arr[i]);
        }
       
        // Return the maximum
        return Math.max(dp1[dp1.length - 1], dp2[dp2.length - 1]);
    }
 
    // Driver Code
    public static void main(String args[]) {
        int arr[] = { 2, 3, 6, 5, 4, 7, 8 };
        int N = arr.length;
 
        System.out.println(maxScore(arr, N));
    }
}
 
// This code is contributed by saurabh_jaiswal.


Python3
# Python Program to implement
# the above approach
INF = 1e9
 
# Function to find the maximum score
# achieved by alternative minus-plus
# of elements of a subsequence in
# the given array
def maxScore(arr, N):
    dp1 = [0] * (N + 1)
    dp2 = [0] * (N + 1)
 
    dp1[0] = -INF
    dp2[0] = 0
 
    for i in range(N):
        dp1[i + 1] = max(dp1[i], dp2[i] + arr[i])
        dp2[i + 1] = max(dp2[i], dp1[i] - arr[i])
 
    # Return the maximum
    return max(dp1[len(dp1) - 1], dp2[len(dp2) - 1])
 
# Driver Code
arr = [2, 3, 6, 5, 4, 7, 8]
N = len(arr)
 
print(maxScore(arr, N))
 
# This code is contributed by Saurabh Jaiswal


C#
// C# code to implement the above approach
using System;
class GFG
{
 
    static double INF = 1E9;
 
    // Function to find the maximum score
    // achieved by alternative minus-plus
    // of elements of a subsequence in
    // the given array
    public static int maxScore(int []arr, int N) {
        int []dp1 = new int[N + 1];
        int []dp2 = new int[N + 1];
 
        dp1[0] = (int) -INF;
        dp2[0] = 0;
 
        for (int i = 0; i < N; ++i) {
            dp1[i + 1] = Math.Max(dp1[i], dp2[i] + arr[i]);
            dp2[i + 1] = Math.Max(dp2[i], dp1[i] - arr[i]);
        }
       
        // Return the maximum
        return Math.Max(dp1[dp1.Length - 1], dp2[dp2.Length - 1]);
    }
 
    // Driver Code
    public static void Main() {
        int []arr = { 2, 3, 6, 5, 4, 7, 8 };
        int N = arr.Length;
 
        Console.Write(maxScore(arr, N));
    }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
10

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