📜  前缀和前缀后给定元素的最大总和增加子序列必须

📅  最后修改于: 2021-06-26 01:32:40             🧑  作者: Mango

给定n个正整数的数组,编写一个程序以查找从前缀到第i个索引的递增子序列的最大和,并且还包括一个在i之后的给定的第k个元素,即k> i。

例子 :

先决条件:最大总和增加子序列

天真的方法:

  1. 构造一个新数组,其中包含直到ith索引和第k个元素的元素。
  2. 递归计算所有递增的子序列。
  3. 丢弃所有不包含第k个元素的子序列。
  4. 从左边的子序列计算最大总和并显示它。

时间复杂度: O(2 n )

更好的方法:使用动态方法来维护表dp [] []。 dp [i] [k]的值存储递增的子序列的最大和,直到第ith个索引并包含第k个元素。

C++
// C++ program to find maximum sum increasing
// subsequence till i-th index and including
// k-th index.
#include 
#define ll long long int
using namespace std;
 
ll pre_compute(ll a[], ll n, ll index, ll k)
{
    ll dp[n][n] = { 0 };
 
    // Initializing the first row of the dp[][].
    for (int i = 0; i < n; i++) {
        if (a[i] > a[0])
            dp[0][i] = a[i] + a[0];       
        else
            dp[0][i] = a[i];       
    }
 
    // Creating the dp[][] matrix.
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (a[j] > a[i] && j > i) {
                if (dp[i - 1][i] + a[j] > dp[i - 1][j])
                    dp[i][j] = dp[i - 1][i] + a[j];               
                else
                    dp[i][j] = dp[i - 1][j];
            }
            else
                dp[i][j] = dp[i - 1][j];           
        }
    }
 
    // To calculate for i=4 and k=6.
    return dp[index][k];
}
 
int main()
{
    ll a[] = { 1, 101, 2, 3, 100, 4, 5 };
    ll n = sizeof(a) / sizeof(a[0]);
    ll index = 4, k = 6;
    printf("%lld", pre_compute(a, n, index, k));
    return 0;
}


Java
// Java program to find maximum sum increasing
// subsequence tiint i-th index and including
// k-th index.
class GFG {
     
    static int pre_compute(int a[], int n,
                             int index, int k)
    {
        int dp[][] = new int[n][n];
     
        // Initializing the first row of
        // the dp[][].
        for (int i = 0; i < n; i++) {
            if (a[i] > a[0])
                dp[0][i] = a[i] + a[0];
            else
                dp[0][i] = a[i];    
        }
     
        // Creating the dp[][] matrix.
        for (int i = 1; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (a[j] > a[i] && j > i)
                {
                    if (dp[i - 1][i] + a[j] >
                                 dp[i - 1][j])
                        dp[i][j] = dp[i - 1][i]
                                        + a[j];        
                    else
                        dp[i][j] = dp[i - 1][j];
                }
                else
                    dp[i][j] = dp[i - 1][j];        
            }
        }
     
        // To calculate for i=4 and k=6.
        return dp[index][k];
    }
     
    // Driver code
    public static void main(String[] args)
    {
        int a[] = { 1, 101, 2, 3, 100, 4, 5 };
        int n = a.length;
        int index = 4, k = 6;
        System.out.println(
                  pre_compute(a, n, index, k));
    }
}
 
// This code is contributed by Smitha.


Python3
# Python3 program to find maximum
# sum increasing subsequence till 
# i-th index and including k-th index.
 
def pre_compute(a, n, index, k):
     
    dp = [[0 for i in range(n)]
             for i in range(n)]
              
    # Initializing the first
    # row of the dp[][]
    for i in range(n):
        if a[i] > a[0]:
            dp[0][i] = a[i] + a[0]
        else:
            dp[0][i] = a[i]
             
    # Creating the dp[][] matrix.
    for i in range(1, n):
        for j in range(n):
            if a[j] > a[i] and j > i:
                if dp[i - 1][i] + a[j] > dp[i - 1][j]:
                    dp[i][j] = dp[i - 1][i] + a[j]
                else:
                    dp[i][j] = dp[i - 1][j]
            else:
                dp[i][j] = dp[i - 1][j]
                 
    # To calculate for i=4 and k=6.
    return dp[index][k]
 
# Driver code
a = [1, 101, 2, 3, 100, 4, 5 ]
n = len(a)
index = 4
k = 6
print(pre_compute(a, n, index, k))
 
# This code is contributed
# by sahilshelangia


C#
// C# program to find maximum
// sum increasing subsequence
// till i-th index and including
// k-th index.
using System;
 
class GFG
{
    static int pre_compute(int []a, int n,
                           int index, int k)
    {
    int [,]dp = new int[n, n];
 
    // Initializing the first
    // row of the dp[][].
    for (int i = 0; i < n; i++)
    {
        if (a[i] > a[0])
            dp[0, i] = a[i] + a[0];
        else
            dp[0, i] = a[i];
    }
 
    // Creating the dp[][] matrix.
    for (int i = 1; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (a[j] > a[i] && j > i)
            {
                if (dp[i - 1, i] + a[j] >
                            dp[i - 1, j])
                    dp[i, j] = dp[i - 1, i] +
                                        a[j];    
                else
                    dp[i, j] = dp[i - 1, j];
            }
            else
                dp[i, j] = dp[i - 1, j];        
        }
    }
 
    // To calculate for i=4 and k=6.
    return dp[index, k];
}
 
// Driver code
static public void Main ()
{
    int []a = {1, 101, 2,
               3, 100, 4, 5};
    int n = a.Length;
    int index = 4, k = 6;
    Console.WriteLine(pre_compute(a, n,
                                  index, k));
}
}
 
// This code is contributed by @ajit


PHP
 $a[0])
            $dp[0][$i] = $a[$i] + $a[0];    
        else
            $dp[0][$i] = $a[$i];    
    }
 
    // Creating the dp[][] matrix.
    for ($i = 1; $i < $n; $i++)
    {
        for ($j = 0; $j < $n; $j++)
        {
            if ($a[$j] > $a[$i] && $j > $i)
            {
                if (($dp[$i - 1][$i] + $a[$j]) >
                                 $dp[$i - 1][$j])
                    $dp[$i][$j] = $dp[$i - 1][$i] +
                                           $a[$j];            
                else
                    $dp[$i][$j] = $dp[$i - 1][$j];
            }
            else
                $dp[$i][$j] = $dp[$i - 1][$j];        
        }
    }
 
    // To calculate for i=4 and k=6.
    return $dp[$index][$k];
}
 
// Driver Code
$a = array( 1, 101, 2, 3, 100, 4, 5 );
$n = sizeof($a);
$index = 4;
$k = 6;
echo pre_compute($a, $n, $index, $k);
 
// This code is contributed by ita_c
?>


Javascript


C++
// C++ program for the above approach
#include 
#include 
using namespace std;
 
// Fucntion to find the
// maximum of two numbers
int max(int a, int b)
{
    if (a > b) {
        return a;
    }
    return b;
}
 
// Function to find the sum
int pre_compute(int a[], int n, int index, int k)
{
    // Base case
    if (index >= k) {
        return -1;
    }
    // Initialize the dp table
    int dp[index] = { 0 };
 
    int i;
 
    // Initialize the dp array with
    // corresponding array index value
    for (i = 0; i <= index; i++) {
        dp[i] = a[i];
    }
 
    int maxi = INT_MIN;
 
    for (i = 0; i <= index; i++) {
        // Only include values
        // which are less than a[k]
        if (a[i] >= a[k]) {
            continue;
        }
 
        for (int j = 0; j < i; j++) {
            // Check if a[i] is
            // greater than a[j]
            if (a[i] > a[j]) {
                dp[i] = dp[j] + a[i];
            }
 
            // Update maxi
            maxi = max(maxi, dp[i]);
        }
    }
   
    // Incase all the elements in
    // the array upto ith index
    // are greater or equal to a[k]
    if (maxi == INT_MIN) {
        return a[k];
    }
    return maxi + a[k];
    // Contributed by Mainak Dutta
}
 
// Driver code
int main()
{
    int a[] = { 1, 101, 2, 3, 100, 4, 5 };
    int n = sizeof(a) / sizeof(a[0]);
    int index = 4, k = 6;
 
    // Function call
    printf("%d", pre_compute(a, n, index, k));
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Fucntion to find the
// maximum of two numbers
static int max(int a, int b)
{
    if (a > b)
    {
        return a;
    }
    return b;
}
  
// Function to find the sum
static int pre_compute(int a[], int n,
                       int index, int k)
{
     
    // Base case
    if (index >= k)
    {
        return -1;
    }
     
    // Initialize the dp table
    int[] dp = new int[index + 1];
  
    int i;
  
    // Initialize the dp array with
    // corresponding array index value
    for(i = 0; i <= index; i++)
    {
        dp[i] = a[i];
    }
  
    int maxi = Integer.MIN_VALUE;
  
    for(i = 0; i <= index; i++)
    {
         
        // Only include values
        // which are less than a[k]
        if (a[i] >= a[k])
        {
            continue;
        }
  
        for(int j = 0; j < i; j++)
        {
             
            // Check if a[i] is
            // greater than a[j]
            if (a[i] > a[j])
            {
                dp[i] = dp[j] + a[i];
            }
  
            // Update maxi
            maxi = max(maxi, dp[i]);
        }
    }
     
    // Incase all the elements in
    // the array upto ith index
    // are greater or equal to a[k]
    if (maxi == Integer.MIN_VALUE)
    {
        return a[k];
    }
    return maxi + a[k];
}
  
// Driver code
public static void main (String[] args)
{
    int a[] = { 1, 101, 2, 3, 100, 4, 5 };
    int n = a.length;
    int index = 4, k = 6;
     
    System.out.println(pre_compute(a, n, index, k));
}
}
 
// This code is contributed by rag2127


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Fucntion to find the
// maximum of two numbers
static int max(int a, int b)
{
    if (a > b)
    {
        return a;
    }
    return b;
}
   
// Function to find the sum
static int pre_compute(int[] a, int n,
                       int index, int k)
{
     
    // Base case
    if (index >= k)
    {
        return -1;
    }
      
    // Initialize the dp table
    int[] dp = new int[index + 1];
   
    int i;
   
    // Initialize the dp array with
    // corresponding array index value
    for(i = 0; i <= index; i++)
    {
        dp[i] = a[i];
    }
   
    int maxi = Int32.MinValue;
   
    for(i = 0; i <= index; i++)
    {
          
        // Only include values
        // which are less than a[k]
        if (a[i] >= a[k])
        {
            continue;
        }
   
        for(int j = 0; j < i; j++)
        {
              
            // Check if a[i] is
            // greater than a[j]
            if (a[i] > a[j])
            {
                dp[i] = dp[j] + a[i];
            }
   
            // Update maxi
            maxi = Math.Max(maxi, dp[i]);
        }
    }
      
    // Incase all the elements in
    // the array upto ith index
    // are greater or equal to a[k]
    if (maxi == Int32.MinValue)
    {
        return a[k];
    }
    return maxi + a[k];
}
   
// Driver code
static public void Main()
{
    int[] a = { 1, 101, 2, 3, 100, 4, 5 };
    int n = a.Length;
    int index = 4, k = 6;
  
    Console.WriteLine(pre_compute(a, n, index, k));
}
}
 
// This code is contributed by avanitrachhadiya2155


Javascript


输出
11

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

高效的方法:这个问题基本上是找到子序列的所有元素都小于第k个(索引)元素或arr [k],直到给定索引i为止,递增子序列的最大和。因此,找到最大和增加子序列。

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
#include 
using namespace std;
 
// Fucntion to find the
// maximum of two numbers
int max(int a, int b)
{
    if (a > b) {
        return a;
    }
    return b;
}
 
// Function to find the sum
int pre_compute(int a[], int n, int index, int k)
{
    // Base case
    if (index >= k) {
        return -1;
    }
    // Initialize the dp table
    int dp[index] = { 0 };
 
    int i;
 
    // Initialize the dp array with
    // corresponding array index value
    for (i = 0; i <= index; i++) {
        dp[i] = a[i];
    }
 
    int maxi = INT_MIN;
 
    for (i = 0; i <= index; i++) {
        // Only include values
        // which are less than a[k]
        if (a[i] >= a[k]) {
            continue;
        }
 
        for (int j = 0; j < i; j++) {
            // Check if a[i] is
            // greater than a[j]
            if (a[i] > a[j]) {
                dp[i] = dp[j] + a[i];
            }
 
            // Update maxi
            maxi = max(maxi, dp[i]);
        }
    }
   
    // Incase all the elements in
    // the array upto ith index
    // are greater or equal to a[k]
    if (maxi == INT_MIN) {
        return a[k];
    }
    return maxi + a[k];
    // Contributed by Mainak Dutta
}
 
// Driver code
int main()
{
    int a[] = { 1, 101, 2, 3, 100, 4, 5 };
    int n = sizeof(a) / sizeof(a[0]);
    int index = 4, k = 6;
 
    // Function call
    printf("%d", pre_compute(a, n, index, k));
    return 0;
}

Java

// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Fucntion to find the
// maximum of two numbers
static int max(int a, int b)
{
    if (a > b)
    {
        return a;
    }
    return b;
}
  
// Function to find the sum
static int pre_compute(int a[], int n,
                       int index, int k)
{
     
    // Base case
    if (index >= k)
    {
        return -1;
    }
     
    // Initialize the dp table
    int[] dp = new int[index + 1];
  
    int i;
  
    // Initialize the dp array with
    // corresponding array index value
    for(i = 0; i <= index; i++)
    {
        dp[i] = a[i];
    }
  
    int maxi = Integer.MIN_VALUE;
  
    for(i = 0; i <= index; i++)
    {
         
        // Only include values
        // which are less than a[k]
        if (a[i] >= a[k])
        {
            continue;
        }
  
        for(int j = 0; j < i; j++)
        {
             
            // Check if a[i] is
            // greater than a[j]
            if (a[i] > a[j])
            {
                dp[i] = dp[j] + a[i];
            }
  
            // Update maxi
            maxi = max(maxi, dp[i]);
        }
    }
     
    // Incase all the elements in
    // the array upto ith index
    // are greater or equal to a[k]
    if (maxi == Integer.MIN_VALUE)
    {
        return a[k];
    }
    return maxi + a[k];
}
  
// Driver code
public static void main (String[] args)
{
    int a[] = { 1, 101, 2, 3, 100, 4, 5 };
    int n = a.length;
    int index = 4, k = 6;
     
    System.out.println(pre_compute(a, n, index, k));
}
}
 
// This code is contributed by rag2127

C#

// C# program for the above approach
using System;
 
class GFG{
 
// Fucntion to find the
// maximum of two numbers
static int max(int a, int b)
{
    if (a > b)
    {
        return a;
    }
    return b;
}
   
// Function to find the sum
static int pre_compute(int[] a, int n,
                       int index, int k)
{
     
    // Base case
    if (index >= k)
    {
        return -1;
    }
      
    // Initialize the dp table
    int[] dp = new int[index + 1];
   
    int i;
   
    // Initialize the dp array with
    // corresponding array index value
    for(i = 0; i <= index; i++)
    {
        dp[i] = a[i];
    }
   
    int maxi = Int32.MinValue;
   
    for(i = 0; i <= index; i++)
    {
          
        // Only include values
        // which are less than a[k]
        if (a[i] >= a[k])
        {
            continue;
        }
   
        for(int j = 0; j < i; j++)
        {
              
            // Check if a[i] is
            // greater than a[j]
            if (a[i] > a[j])
            {
                dp[i] = dp[j] + a[i];
            }
   
            // Update maxi
            maxi = Math.Max(maxi, dp[i]);
        }
    }
      
    // Incase all the elements in
    // the array upto ith index
    // are greater or equal to a[k]
    if (maxi == Int32.MinValue)
    {
        return a[k];
    }
    return maxi + a[k];
}
   
// Driver code
static public void Main()
{
    int[] a = { 1, 101, 2, 3, 100, 4, 5 };
    int n = a.Length;
    int index = 4, k = 6;
  
    Console.WriteLine(pre_compute(a, n, index, k));
}
}
 
// This code is contributed by avanitrachhadiya2155

Java脚本


输出
11

时间复杂度: O(指标2 )
辅助空间: O(index)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。