📌  相关文章
📜  取长度为K的非重叠子数组后最大化数组和

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

给定一个长度为N的整数数组arr []和一个整数K ,任务是选择一些不重叠的子数组,使得每个子数组的长度都恰好为K ,没有两个子数组是相邻的并且是所有子数组的总和所选子数组的元素最大。

例子:

方法:可以使用动态编程解决此问题。假设我们在索引i处。令dp [i]定义为满足上述条件的子阵列arr [i…n-1]的所有可能子集的元素的最大和。
我们将有两个可能的选择,即选择子数组arr [i…i + k-1]并求解dp [i + k + 1]或拒绝它并求解dp [i + 1]

因此,递归关系将是

由于K的值可能很大,因此我们将使用前缀和数组来查找O(1)中子数组arr [i…i + k – 1]的所有元素的和。
总体而言,该算法的时间复杂度将为O(N)

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
#define maxLen 10
using namespace std;
  
// To store the states of dp
int dp[maxLen];
  
// To check if a given state
// has been solved
bool v[maxLen];
  
// To store the prefix-sum
int prefix_sum[maxLen];
  
// Function to fill the prefix_sum[] with
// the prefix sum of the given array
void findPrefixSum(int arr[], int n)
{
    prefix_sum[0] = arr[0];
    for (int i = 1; i < n; i++)
        prefix_sum[i] = arr[i] + prefix_sum[i - 1];
}
  
// Function to find the maximum sum subsequence
// such that no two elements are adjacent
int maxSum(int arr[], int i, int n, int k)
{
    // Base case
    if (i + k > n)
        return 0;
  
    // To check if a state has
    // been solved
    if (v[i])
        return dp[i];
    v[i] = 1;
  
    int x;
  
    if (i == 0)
        x = prefix_sum[k - 1];
    else
        x = prefix_sum[i + k - 1] - prefix_sum[i - 1];
  
    // Required recurrence relation
    dp[i] = max(maxSum(arr, i + 1, n, k),
                x + maxSum(arr, i + k + 1, n, k));
  
    // Returning the value
    return dp[i];
}
  
// Driver code
int main()
{
    int arr[] = { 1, 3, 7, 6 };
    int n = sizeof(arr) / sizeof(int);
    int k = 1;
  
    // Finding prefix-sum
    findPrefixSum(arr, n);
  
    // Finding the maximum possible sum
    cout << maxSum(arr, 0, n, k);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG 
{
  
    static int maxLen = 10;
  
    // To store the states of dp
    static int[] dp = new int[maxLen];
  
    // To check if a given state
    // has been solved
    static boolean[] v = new boolean[maxLen];
  
    // To store the prefix-sum
    static int[] prefix_sum = new int[maxLen];
  
    // Function to fill the prefix_sum[] with
    // the prefix sum of the given array
    static void findPrefixSum(int arr[], int n) 
    {
        prefix_sum[0] = arr[0];
        for (int i = 1; i < n; i++)
        {
            prefix_sum[i] = arr[i] + prefix_sum[i - 1];
        }
    }
  
    // Function to find the maximum sum subsequence
    // such that no two elements are adjacent
    static int maxSum(int arr[], int i, int n, int k)
    {
        // Base case
        if (i + k > n) 
        {
            return 0;
        }
  
        // To check if a state has
        // been solved
        if (v[i]) 
        {
            return dp[i];
        }
        v[i] = true;
  
        int x;
  
        if (i == 0)
        {
            x = prefix_sum[k - 1];
        } 
        else
        {
            x = prefix_sum[i + k - 1] - prefix_sum[i - 1];
        }
  
        // Required recurrence relation
        dp[i] = Math.max(maxSum(arr, i + 1, n, k),
                x + maxSum(arr, i + k + 1, n, k));
  
        // Returning the value
        return dp[i];
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = {1, 3, 7, 6};
        int n = arr.length;
        int k = 1;
  
        // Finding prefix-sum
        findPrefixSum(arr, n);
  
        // Finding the maximum possible sum
        System.out.println(maxSum(arr, 0, n, k));
    }
}
  
// This code contributed by Rajput-Ji


Python3
# Python3 implementation of the approach 
  
maxLen = 10
  
# To store the states of dp 
dp = [0]*maxLen; 
  
# To check if a given state 
# has been solved 
v = [0]*maxLen; 
  
# To store the prefix-sum 
prefix_sum = [0]*maxLen; 
  
# Function to fill the prefix_sum[] with 
# the prefix sum of the given array 
def findPrefixSum(arr, n) : 
  
    prefix_sum[0] = arr[0]; 
    for i in range(n) :
        prefix_sum[i] = arr[i] + prefix_sum[i - 1]; 
  
  
# Function to find the maximum sum subsequence 
# such that no two elements are adjacent 
def maxSum(arr, i, n, k) : 
  
    # Base case 
    if (i + k > n) :
        return 0; 
  
    # To check if a state has 
    # been solved 
    if (v[i]) :
        return dp[i]; 
          
    v[i] = 1; 
  
    if (i == 0) :
        x = prefix_sum[k - 1]; 
    else :
        x = prefix_sum[i + k - 1] - prefix_sum[i - 1]; 
  
    # Required recurrence relation 
    dp[i] = max(maxSum(arr, i + 1, n, k), 
                x + maxSum(arr, i + k + 1, n, k)); 
  
    # Returning the value 
    return dp[i]; 
  
  
# Driver code 
if __name__ == "__main__" : 
  
    arr = [ 1, 3, 7, 6 ];
      
    n = len(arr); 
    k = 1; 
  
    # Finding prefix-sum 
    findPrefixSum(arr, n); 
  
    # Finding the maximum possible sum 
    print(maxSum(arr, 0, n, k)); 
      
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
  
class GFG 
{ 
  
    static int maxLen = 10; 
  
    // To store the states of dp 
    static int[] dp = new int[maxLen]; 
  
    // To check if a given state 
    // has been solved 
    static bool[] v = new bool[maxLen]; 
  
    // To store the prefix-sum 
    static int[] prefix_sum = new int[maxLen]; 
  
    // Function to fill the prefix_sum[] with 
    // the prefix sum of the given array 
    static void findPrefixSum(int []arr, int n) 
    { 
        prefix_sum[0] = arr[0]; 
        for (int i = 1; i < n; i++) 
        { 
            prefix_sum[i] = arr[i] + prefix_sum[i - 1]; 
        } 
    } 
  
    // Function to find the maximum sum subsequence 
    // such that no two elements are adjacent 
    static int maxSum(int []arr, int i, int n, int k) 
    { 
        // Base case 
        if (i + k > n) 
        { 
            return 0; 
        } 
  
        // To check if a state has 
        // been solved 
        if (v[i]) 
        { 
            return dp[i]; 
        } 
        v[i] = true; 
  
        int x; 
  
        if (i == 0) 
        { 
            x = prefix_sum[k - 1]; 
        } 
        else
        { 
            x = prefix_sum[i + k - 1] - prefix_sum[i - 1]; 
        } 
  
        // Required recurrence relation 
        dp[i] = Math.Max(maxSum(arr, i + 1, n, k), 
                x + maxSum(arr, i + k + 1, n, k)); 
  
        // Returning the value 
        return dp[i]; 
    } 
  
    // Driver code 
    public static void Main(String[] args) 
    { 
        int []arr = {1, 3, 7, 6}; 
        int n = arr.Length; 
        int k = 1; 
  
        // Finding prefix-sum 
        findPrefixSum(arr, n); 
  
        // Finding the maximum possible sum 
        Console.Write(maxSum(arr, 0, n, k)); 
    } 
} 
  
// This code is contributed by Princi Singh


输出:
9