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

📅  最后修改于: 2021-09-22 09:41:47             🧑  作者: 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
?>


Javascript


输出:
45

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程