📜  子序列增加的最大乘积

📅  最后修改于: 2021-04-24 17:17:45             🧑  作者: Mango

给定一个数字数组,求出该数组的递增子序列的数字乘以该乘积所形成的最大乘积。
注意:单个数字应该是大小为1的递增子序列。
例子:

Input : arr[] = { 3, 100, 4, 5, 150, 6 }
Output : 45000
Maximum product is 45000 formed by the 
increasing subsequence 3, 100, 150. Note
that the longest increasing subsequence 
is different {3, 4, 5, 6}

Input : arr[] = { 10, 22, 9, 33, 21, 50, 41, 60 }
Output : 21780000
Maximum product is 21780000 formed by the 
increasing subsequence 10, 22, 33, 50, 60.
          

先决条件:最长递增子序列
方法:使用动态方法来维护表mpis []。 mpis [i]的值存储以arr [i]结尾的乘积最大乘积。最初,所有递增子序列表的值都初始化为arr [i]。我们使用类似于LIS问题的递归方法来找到结果。

C++
/* Dynamic programming C++ implementation of maximum
   product of an increasing subsequence */
#include 
#define ll long long int
using namespace std;
 
// Returns product of maximum product increasing
// subsequence.
ll lis(ll arr[], ll n)
{
    ll mpis[n];
 
    /* Initialize MPIS values */
    for (int i = 0; i < n; i++)
        mpis[i] = arr[i];
 
    /* Compute optimized MPIS values considering
       every element as ending element of sequence */
    for (int i = 1; i < n; i++)
        for (int j = 0; j < i; j++)
            if (arr[i] > arr[j] && mpis[i] < (mpis[j] * arr[i]))
                mpis[i] = mpis[j] * arr[i];
 
    /* Pick maximum of all product values */
    return *max_element(mpis, mpis + n);
}
 
/* Driver program to test above function */
int main()
{
    ll arr[] = { 3, 100, 4, 5, 150, 6 };
    ll n = sizeof(arr) / sizeof(arr[0]);
    printf("%lld", lis(arr, n));
    return 0;
}


Java
/* Dynamic programming Java implementation
of maximum product of an increasing
subsequence */
import java.util.Arrays;
import java.util.Collections;
 
class GFG {
 
    // Returns product of maximum product
    // increasing subsequence.
    static int lis(int[] arr, int n)
    {
        int[] mpis = new int[n];
        int max = Integer.MIN_VALUE;
         
        /* Initialize MPIS values */
        for (int i = 0; i < n; i++)
            mpis[i] = arr[i];
 
        /* Compute optimized MPIS values
        considering every element as ending
        element of sequence */
        for (int i = 1; i < n; i++)
            for (int j = 0; j < i; j++)
                if (arr[i] > arr[j] && mpis[i]
                         < (mpis[j] * arr[i]))
                    mpis[i] = mpis[j] * arr[i];
 
        /* Pick maximum of all product values
        using for loop*/
        for (int k = 0; k < mpis.length; k++)
        {
            if (mpis[k] > max) {
                max = mpis[k];
            }
        }
         
        return max;
    }
 
    // Driver program to test above function
    static public void main(String[] args)
    {
 
        int[] arr = { 3, 100, 4, 5, 150, 6 };
        int n = arr.length;
 
        System.out.println(lis(arr, n));
    }
}
 
// This code is contributed by parashar.
Python3 highlight=# Dynamic programming Python3 implementation
# of maximum product of an increasing
# subsequence 

# Returns product of maximum product
# increasing subsequence.
def lis (arr, n ):
    mpis =[0] * (n)
    
    # Initialize MPIS values
    for i in range(n):
        mpis[i] = arr[i]
    
    # Compute optimized MPIS values
    # considering every element as 
    # ending element of sequence
    for i in range(1, n):
        for j in range(i):
            if (arr[i] > arr[j] and
                    mpis[i] < (mpis[j] * arr[i])):
                        mpis[i] = mpis[j] * arr[i]
    
    # Pick maximum of all product values 
    return max(mpis)

# Driver code to test above function
arr = [3, 100, 4, 5, 150, 6]
n = len(arr)
print( lis(arr, n))

# This code is contributed by "Sharad_Bhardwaj".


C#
/* Dynamic programming C# implementation
of maximum product of an increasing
subsequence */
using System;
using System.Linq;
 
public class GFG {
 
    // Returns product of maximum product
    // increasing subsequence.
    static long lis(long[] arr, long n)
    {
        long[] mpis = new long[n];
 
        /* Initialize MPIS values */
        for (int i = 0; i < n; i++)
            mpis[i] = arr[i];
 
        /* Compute optimized MPIS values considering
        every element as ending element of sequence */
        for (int i = 1; i < n; i++)
            for (int j = 0; j < i; j++)
                if (arr[i] > arr[j] && mpis[i] < (mpis[j] * arr[i]))
                    mpis[i] = mpis[j] * arr[i];
 
        /* Pick maximum of all product values */
        return mpis.Max();
    }
 
    /* Driver program to test above function */
    static public void Main()
    {
 
        long[] arr = { 3, 100, 4, 5, 150, 6 };
        long n = arr.Length;
 
        Console.WriteLine(lis(arr, n));
    }
}
 
// This code is contributed by vt_m.


PHP
 $arr[$j] && $mpis[$i] < ($mpis[$j] * $arr[$i]))
                $mpis[$i] = $mpis[$j] * $arr[$i];
  
    /* Pick maximum of all product values */
    return max($mpis);
}
  
/* Driver program to test above function */
 
    $arr = array ( 3, 100, 4, 5, 150, 6 );
    $n = sizeof($arr) / sizeof($arr[0]);
    echo lis($arr, $n);
    return 0;
?>


Javascript


输出:

45000

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