📌  相关文章
📜  子序列可能的最大和,使得在数组中的距离 < K 处没有两个元素出现

📅  最后修改于: 2021-09-17 07:43:51             🧑  作者: Mango

给定一个由n 个整数组成的数组arr[]和一个整数k ,任务是找到子序列可能的最大和,使得子序列中没有两个元素出现在原始数组中距离≤ k 处
例子:

方法:在选择索引i处的元素时,我们有两种选择,要么将当前元素包含在子序列中,要么不包含。让dp[i]表示到目前为止到达索引i处的元素的最大总和。我们可以计算dp[i]的值如下:

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the maximum sum possible
int maxSum(int* arr, int k, int n)
{
    if (n == 0)
        return 0;
    if (n == 1)
        return arr[0];
    if (n == 2)
        return max(arr[0], arr[1]);
 
    // dp[i] represent the maximum sum so far
    // after reaching current position i
    int dp[n];
 
    // Initialize dp[0]
    dp[0] = arr[0];
 
    // Initialize the dp values till k since any
    // two elements included in the sub-sequence
    // must be atleast k indices apart, and thus
    // first element and second element
    // will be k indices apart
    for (int i = 1; i <= k; i++)
        dp[i] = max(arr[i], dp[i - 1]);
 
    // Fill remaining positions
    for (int i = k + 1; i < n; i++)
        dp[i] = max(arr[i], dp[i - (k + 1)] + arr[i]);
 
    // Return the maximum sum
    int max = *(std::max_element(dp, dp + n));
    return max;
}
 
// Driver code
int main()
{
    int arr[] = { 6, 7, 1, 3, 8, 2, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 2;
    cout << maxSum(arr, k, n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
// Function to return the maximum sum possible
static int maxSum(int []arr, int k, int n)
{
    if (n == 0)
        return 0;
    if (n == 1)
        return arr[0];
    if (n == 2)
        return Math.max(arr[0], arr[1]);
 
    // dp[i] represent the maximum sum so far
    // after reaching current position i
    int[] dp = new int[n];
 
    // Initialize dp[0]
    dp[0] = arr[0];
 
    // Initialize the dp values till k since any
    // two elements included in the sub-sequence
    // must be atleast k indices apart, and thus
    // first element and second element
    // will be k indices apart
    for (int i = 1; i <= k; i++)
        dp[i] = Math.max(arr[i], dp[i - 1]);
 
    // Fill remaining positions
    for (int i = k + 1; i < n; i++)
        dp[i] = Math.max(arr[i], dp[i - (k + 1)] + arr[i]);
 
    // Return the maximum sum
    return maximum(dp);
}
 
static int maximum(int[] arr)
{
    int max = Integer.MIN_VALUE;
    for(int i = 0; i < arr.length; i++)
    {
        if(arr[i] > max)
        {
            max = arr[i];
        }
    }
    return max;
}
 
// Driver code
public static void main (String[] args)
{
    int []arr = { 6, 7, 1, 3, 8, 2, 4 };
    int n = arr.length;
    int k = 2;
    System.out.println(maxSum(arr, k, n));
}
}
 
// This code is contributed by mits


Python3
# Python3 implementation of the approach
 
# Function to return the
# maximum sum possible
def maxSum(arr, k, n) :
     
    if (n == 0) :
        return 0;
    if (n == 1) :
        return arr[0];
    if (n == 2) :
        return max(arr[0], arr[1]);
 
    # dp[i] represent the maximum sum so far
    # after reaching current position i
    dp = [0] * n ;
 
    # Initialize dp[0]
    dp[0] = arr[0];
 
    # Initialize the dp values till k since any
    # two elements included in the sub-sequence
    # must be atleast k indices apart, and thus
    # first element and second element
    # will be k indices apart
    for i in range(1, k + 1) :
        dp[i] = max(arr[i], dp[i - 1]);
 
    # Fill remaining positions
    for i in range(k + 1, n) :
        dp[i] = max(arr[i],
                    dp[i - (k + 1)] + arr[i]);
 
    # Return the maximum sum
    max_element = max(dp);
    return max_element;
 
# Driver code
if __name__ == "__main__" :
    arr = [ 6, 7, 1, 3, 8, 2, 4 ];
    n = len(arr);
    k = 2;
     
    print(maxSum(arr, k, n));
     
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
using System.Linq;
 
class GFG
{
     
// Function to return the maximum sum possible
static int maxSum(int []arr, int k, int n)
{
    if (n == 0)
        return 0;
    if (n == 1)
        return arr[0];
    if (n == 2)
        return Math.Max(arr[0], arr[1]);
 
    // dp[i] represent the maximum sum so far
    // after reaching current position i
    int[] dp = new int[n];
 
    // Initialize dp[0]
    dp[0] = arr[0];
 
    // Initialize the dp values till k since any
    // two elements included in the sub-sequence
    // must be atleast k indices apart, and thus
    // first element and second element
    // will be k indices apart
    for (int i = 1; i <= k; i++)
        dp[i] = Math.Max(arr[i], dp[i - 1]);
 
    // Fill remaining positions
    for (int i = k + 1; i < n; i++)
        dp[i] = Math.Max(arr[i], dp[i - (k + 1)] + arr[i]);
 
    // Return the maximum sum
    int max = dp.Max();
    return max;
}
 
// Driver code
static void Main()
{
    int []arr = { 6, 7, 1, 3, 8, 2, 4 };
    int n = arr.Length;
    int k = 2;
    Console.WriteLine(maxSum(arr, k, n));
}
}
 
// This code is contributed by mits


Javascript


输出:
15

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

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