📌  相关文章
📜  通过给定的移动从数组中最大化可能的总和

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

给定三个整数N、MK和一个由N 个整数组成的数组a[] ,其中MK表示可能移动的总数和当前元素左侧可能移动的次数(按索引移动)一个数组,任务是通过利用所有可用的移动遍历数组来最大化可能的总和。

例子:

方法:上述问题可以使用动态规划解决。请按照以下步骤解决问题:

  • 初始化一个dp[][]矩阵,使得dp[i][j]通过使用j 次向左移动来存储可能达到i索引的最大和。
  • 可以看出,只有当i ≥ 1k > 0时才可能向左移动,如果i < n – 1时才可能向右移动
  • 检查条件并更新上述两次移动中可能的和的最大值并存储在dp[i][j] 中

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
const int k = 1;
const int m = 4;
 
// Function to find the maximum sum possible
// by given moves from the array
int maxValue(int a[], int n, int pos,
             int moves, int left,
             int dp[][k + 1])
{
    // Checking for boundary
    if (moves == 0 || (pos > n - 1 || pos < 0))
        return 0;
 
    // If previously computed subproblem occurs
    if (dp[pos][left] != -1)
        return dp[pos][left];
 
    int value = 0;
 
    // If element can be moved left
    if (left > 0 && pos >= 1)
 
        // Calculate maximum possible sum
        // by moving left from current index
        value = max(value, a[pos] +
                    maxValue(a, n, pos - 1, moves - 1,
                             left - 1, dp));
 
    // If element can be moved right
    if (pos <= n - 1)
 
        // Calculate maximum possible sum
        // by moving right from current index
        // and update the maximum sum
        value = max(value, a[pos] +
                    maxValue(a, n, pos + 1,
                             moves - 1, left, dp));
 
    // Store the maximum sum
    return dp[pos][left] = value;
}
 
// Driver Code
int main()
{
    int n = 5;
    int a[] = { 1, 5, 4, 3, 2 };
 
    int dp[n + 1][k + 1];
    memset(dp, -1, sizeof(dp));
    cout << (a[0] + maxValue(a, n, 1, m, k, dp))
         << endl;
}
 
// This code is contributed by sapnasingh4991


Java
// Java Program to implement
// the above approach
import java.io.*;
import java.util.*;
 
public class GFG {
 
    // Function to find the maximum sum possible
    // by given moves from the array
    public static int maxValue(int a[], int n, int pos,
                               int moves, int left,
                               int dp[][])
    {
        // Checking for boundary
        if (moves == 0 || (pos > n - 1 || pos < 0))
            return 0;
 
        // If previously computed subproblem occurs
        if (dp[pos][left] != -1)
            return dp[pos][left];
 
        int value = 0;
 
        // If element can be moved left
        if (left > 0 && pos >= 1)
 
            // Calculate maximum possible sum
            // by moving left from current index
            value = Math.max(
                value, a[pos] + maxValue(a, n, pos - 1,
                                         moves - 1, left - 1, dp));
 
        // If element can be moved right
        if (pos <= n - 1)
 
            // Calculate maximum possible sum
            // by moving right from current index
            // and update the maximum sum
            value = Math.max(
                value, a[pos]
                           + maxValue(a, n, pos + 1,
                                      moves - 1, left, dp));
 
        // Store the maximum sum
        return dp[pos][left] = value;
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int n = 5;
        int a[] = { 1, 5, 4, 3, 2 };
        int k = 1;
        int m = 4;
 
        int dp[][] = new int[n + 1][k + 1];
        for (int i[] : dp)
            Arrays.fill(i, -1);
 
        System.out.println(
            (a[0] + maxValue(a, n, 1, m, k, dp)));
    }
}


Python3
# Python3 program to implement
# the above approach
 
# Function to find the maximum sum possible
# by given moves from the array
def maxValue(a, n, pos, moves, left, dp):
 
    # Checking for boundary
    if(moves == 0 or (pos > n - 1 or pos < 0)):
        return 0
 
    # If previously computed subproblem occurs
    if(dp[pos][left] != -1):
        return dp[pos][left]
 
    value = 0
 
    # If element can be moved left
    if(left > 0 and pos >= 1):
 
        # Calculate maximum possible sum
        # by moving left from current index
        value = max(value, a[pos] +
                    maxValue(a, n, pos - 1,
                                 moves - 1,
                                  left - 1, dp))
 
    # If element can be moved right
    if(pos <= n - 1):
 
        # Calculate maximum possible sum
        # by moving right from current index
        # and update the maximum sum
        value = max(value, a[pos] +
                    maxValue(a, n, pos + 1,
                                 moves - 1,
                                 left, dp))
                                  
    # Store the maximum sum
    dp[pos][left] = value
 
    return dp[pos][left]
 
# Driver Code
n = 5
a = [ 1, 5, 4, 3, 2 ]
k = 1
m = 4
 
dp = [[-1 for x in range(k + 1)]
          for y in range(n + 1)]
 
# Function call
print(a[0] + maxValue(a, n, 1, m, k, dp))
 
# This code is contributed by Shivam Singh


C#
// C# Program to implement
// the above approach
using System;
class GFG
{
 
  // Function to find the maximum sum possible
  // by given moves from the array
  public static int maxValue(int []a, int n, int pos,
                             int moves, int left,
                             int [,]dp)
  {
    // Checking for boundary
    if (moves == 0 || (pos > n - 1 || pos < 0))
      return 0;
 
    // If previously computed subproblem occurs
    if (dp[pos, left] != -1)
      return dp[pos, left];
 
    int value = 0;
 
    // If element can be moved left
    if (left > 0 && pos >= 1)
 
      // Calculate maximum possible sum
      // by moving left from current index
      value = Math.Max(
      value, a[pos] + maxValue(a, n, pos - 1,
                               moves - 1,
                               left - 1, dp));
 
    // If element can be moved right
    if (pos <= n - 1)
 
      // Calculate maximum possible sum
      // by moving right from current index
      // and update the maximum sum
      value = Math.Max(
      value, a[pos] + maxValue(a, n, pos + 1,
                                 moves - 1,
                               left, dp));
 
    // Store the maximum sum
    return dp[pos, left] = value;
  }
 
  // Driver Code
  public static void Main(String []args)
  {
    int n = 5;
    int []a = { 1, 5, 4, 3, 2 };
    int k = 1;
    int m = 4;
 
    int [,]dp = new int[n + 1, k + 1];
    for(int i = 0; i <= n; i++)
      for(int j =0; j <= k; j++)
        dp[i, j] = -1;
 
    Console.WriteLine(
     (a[0] + maxValue(a, n, 1, m, k, dp)));
  }
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
19

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

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