📌  相关文章
📜  使得没有两个元素相邻的最大和|套装2

📅  最后修改于: 2021-04-24 03:17:28             🧑  作者: Mango

给定一个正数数组,请找到一个子序列的最大和,并约束该序列中的任何2个数字都不应与该数组相邻。因此3 2 7 10应该返回13(3和10的总和),或者3 2 5 10 7应该返回15(3、5和7的总和)。

例子:

Input :  arr[] = {3, 5, 3} 
Output : 6 
Explanation : 
Selecting indexes 0 and 2 will maximise the sum 
i.e 3+3 = 6

Input : arr[] = {2, 5, 2}
Output : 5

在上一篇文章中,我们已经讨论了解决此问题的有效方法。

但是,我们也可以使用动态编程方法解决此问题。

动态编程方法:让我们确定“ dp”的状态。令dp [i]是从索引“ i”开始并以索引“ N-1”结束的子数组的最大可能和。现在,我们必须找到此状态与低阶状态之间的递归关系。

在这种情况下,对于索引“ i”,我们将有两种选择。

1) Choose the current index:
   In this case, the relation will be dp[i] = arr[i] + dp[i+2]
2) Skip the current index:
   Relation will be dp[i] = dp[i+1]

我们将选择使我们的结果最大化的路径。
因此,最终关系将是:

dp[i] = max(dp[i+2]+arr[i], dp[i+1])

下面是上述方法的实现:

C++
// C++ program to implement above approach
  
#include 
#define maxLen 10
using namespace std;
  
// variable to store states of dp
int dp[maxLen];
  
// variable to check if a given state
// has been solved
bool v[maxLen];
  
// Function to find the maximum sum subsequence
// such that no two elements are adjacent
int maxSum(int arr[], int i, int n)
{
    // Base case
    if (i >= n)
        return 0;
  
    // To check if a state has
    // been solved
    if (v[i])
        return dp[i];
    v[i] = 1;
  
    // Required recurrence relation
    dp[i] = max(maxSum(arr, i + 1, n),
                arr[i] + maxSum(arr, i + 2, n));
  
    // Returning the value
    return dp[i];
}
  
// Driver code
int main()
{
    int arr[] = { 12, 9, 7, 33 };
  
    int n = sizeof(arr) / sizeof(int);
  
    cout << maxSum(arr, 0, n);
  
    return 0;
}


Java
// Java program to implement above approach 
class GFG
{
  
static int maxLen = 10;
  
// variable to store states of dp 
static int dp[] = new int[maxLen]; 
  
// variable to check if a given state 
// has been solved 
static boolean v[] = new boolean[maxLen]; 
  
// Function to find the maximum sum subsequence 
// such that no two elements are adjacent 
static int maxSum(int arr[], int i, int n) 
{ 
    // Base case 
    if (i >= n) 
        return 0; 
  
    // To check if a state has 
    // been solved 
    if (v[i]) 
        return dp[i]; 
    v[i] = true; 
  
    // Required recurrence relation 
    dp[i] = Math.max(maxSum(arr, i + 1, n), 
                arr[i] + maxSum(arr, i + 2, n)); 
  
    // Returning the value 
    return dp[i]; 
} 
  
// Driver code 
public static void main(String args[])
{ 
    int arr[] = { 12, 9, 7, 33 }; 
    int n = arr.length; 
    System.out.println( maxSum(arr, 0, n)); 
}
}
  
// This code is contributed by Arnab Kundu


Python3
# Python 3 program to implement above approach
maxLen = 10
  
# variable to store states of dp
dp = [0 for i in range(maxLen)]
  
# variable to check if a given state 
# has been solved
v = [0 for i in range(maxLen)]
  
# Function to find the maximum sum subsequence
# such that no two elements are adjacent
def maxSum(arr, i, n):
    # Base case
    if (i >= n):
        return 0
  
    # To check if a state has
    # been solved
    if (v[i]):
        return dp[i]
    v[i] = 1
  
    # Required recurrence relation
    dp[i] = max(maxSum(arr, i + 1, n),
            arr[i] + maxSum(arr, i + 2, n))
  
    # Returning the value
    return dp[i]
  
# Driver code
if __name__ == '__main__':
    arr = [12, 9, 7, 33]
  
    n = len(arr)
    print(maxSum(arr, 0, n))
  
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to implement above approach 
using System;
  
class GFG
{
  
static int maxLen = 10;
  
// variable to store states of dp 
static int[] dp = new int[maxLen]; 
  
// variable to check if a given state 
// has been solved 
static bool[] v = new bool[maxLen]; 
  
// Function to find the maximum sum subsequence 
// such that no two elements are adjacent 
static int maxSum(int[] arr, int i, int n) 
{ 
    // Base case 
    if (i >= n) 
        return 0; 
  
    // To check if a state has 
    // been solved 
    if (v[i]) 
        return dp[i]; 
    v[i] = true; 
  
    // Required recurrence relation 
    dp[i] = Math.Max(maxSum(arr, i + 1, n), 
                arr[i] + maxSum(arr, i + 2, n)); 
  
    // Returning the value 
    return dp[i]; 
} 
  
// Driver code 
public static void Main()
{ 
    int[] arr = { 12, 9, 7, 33 }; 
    int n = arr.Length; 
    Console.Write( maxSum(arr, 0, n)); 
}
}
  
// This code is contributed by ChitraNayal


PHP
= $n) 
        return 0; 
  
    // To check if a state has 
    // been solved 
    if ($GLOBALS['v'][$i]) 
        return $GLOBALS['dp'][$i]; 
          
    $GLOBALS['v'][$i] = 1; 
  
    // Required recurrence relation 
    $GLOBALS['dp'][$i] = max(maxSum($arr, $i + 1, $n), 
                $arr[$i] + maxSum($arr, $i + 2, $n)); 
  
    // Returning the value 
    return $GLOBALS['dp'][$i]; 
} 
  
    // Driver code 
    $arr = array( 12, 9, 7, 33 ); 
  
    $n = count($arr); 
  
    echo maxSum($arr, 0, $n); 
  
    // This code is contributed by AnkitRai01
?>


输出:
45