📌  相关文章
📜  数组中的最大和,以使每个元素都与它正好有一个相邻元素

📅  最后修改于: 2021-04-27 16:47:41             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,您可以选择一些索引,以便每个选定的索引都与它相邻的正好是另一个选定的索引,并且选定索引处的元素之和应为最大。

换句话说,任务是从数组中选择元素,这样就不会单独选择单个元素,也不会选择三个连续索引处的元素,并且所选元素的总和应最大。

任务是打印最大化的总和。

例子:

方法:可以使用动态编程来解决此问题。可以将这个问题转换为选择成对的相邻整数,以使没有两对相邻或没有相同的元素
即如果(arr [i],arr [i + 1])是我们选择的一对,则(arr [i + 2],arr [i + 3])(arr [i +1],arr [i + 2])可以选择。
让我们根据上述陈述确定dp的状态。
对于每个索引i ,我们将选择索引ii + 1,即配对或不配对。如果我们做一对,我们将无法选择索引i + 2,因为它将使2个元素与i + 1相邻。因此,我们接下来必须解决i + 3的问题。如果我们不配对,我们将简单地求解i +1
因此,递归关系将是。

总共有N个状态,每个状态需要O(1)时间来解决。因此,时间复杂度将为O(N)

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
#define arrSize 51
using namespace std;
  
// To store the states of dp
int dp[arrSize];
bool v[arrSize];
  
// Function to return the maximized sum
int sumMax(int i, int arr[], int n)
{
    // Base case
    if (i >= n - 1)
        return 0;
  
    // Checks if a state is
    // already solved
    if (v[i])
        return dp[i];
    v[i] = true;
  
    // Recurrence relation
    dp[i] = max(arr[i] + arr[i + 1]
                    + sumMax(i + 3, arr, n),
                sumMax(i + 1, arr, n));
  
    // Return the result
    return dp[i];
}
  
// Driver code
int main()
{
    int arr[] = { 1, 1, 1, 1 };
    int n = sizeof(arr) / sizeof(int);
  
    cout << sumMax(0, arr, n);
  
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
  
class GFG 
{
  
static int arrSize = 51;
  
// To store the states of dp
static int dp[] = new int[arrSize];
static boolean v[] = new boolean[arrSize];
  
// Function to return the maximized sum
static int sumMax(int i, int arr[], int n)
{
    // Base case
    if (i >= n - 1)
        return 0;
  
    // Checks if a state is
    // already solved
    if (v[i])
        return dp[i];
    v[i] = true;
  
    // Recurrence relation
    dp[i] = Math.max(arr[i] + arr[i + 1]
                    + sumMax(i + 3, arr, n),
                sumMax(i + 1, arr, n));
  
    // Return the result
    return dp[i];
}
  
// Driver code
public static void main (String[] args) 
{
    int arr[] = { 1, 1, 1, 1 };
    int n = arr.length;
  
    System.out.println(sumMax(0, arr, n));
}
}
  
// This code is contributed by anuj_67..


Python3
# Python 3 implementation of the approach
arrSize = 51
  
# To store the states of dp
dp = [0 for i in range(arrSize)]
v = [False for i in range(arrSize)]
  
# Function to return the maximized sum
def sumMax(i,arr,n):
    # Base case
    if (i >= n - 1):
        return 0
  
    # Checks if a state is
    # already solved
    if (v[i]):
        return dp[i]
    v[i] = True
    # Recurrence relation
    dp[i] = max(arr[i] + arr[i + 1] + sumMax(i + 3, arr, n),
                                        sumMax(i + 1, arr, n))
    # Return the result
    return dp[i]
  
# Driver code
if __name__ == '__main__':
    arr = [1, 1, 1, 1]
    n = len(arr)
    print(sumMax(0, arr, n))
  
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
  
class GFG 
{
  
static int arrSize = 51;
  
// To store the states of dp
static int []dp = new int[arrSize];
static bool []v = new bool[arrSize];
  
// Function to return the maximized sum
static int sumMax(int i, int []arr, int n)
{
    // Base case
    if (i >= n - 1)
        return 0;
  
    // Checks if a state is
    // already solved
    if (v[i])
        return dp[i];
    v[i] = true;
  
    // Recurrence relation
    dp[i] = Math.Max(arr[i] + arr[i + 1]
                    + sumMax(i + 3, arr, n),
                sumMax(i + 1, arr, n));
  
    // Return the result
    return dp[i];
}
  
// Driver code
public static void Main () 
{
    int []arr = { 1, 1, 1, 1 };
    int n = arr.Length;
  
    Console.WriteLine(sumMax(0, arr, n));
}
}
  
// This code is contributed by anuj_67..


输出:
2