给定大小为N的正整数的数组arr [] ,任务是找到大小为3的双子序列的最大乘积。
Bitonic子序列:子元素按升序先后降序的子序列。对于i
注意:如果找不到这样的元素,则打印-1。
例子:
Input: arr[] = {1, 8, 3, 7, 5, 6, 7}
Output: 126
Explanation:
Bitonic subsequences of size 3 are
{1, 8, 3}, {1, 8, 7}, {1, 8, 5}, {1, 8, 6}, {1, 7, 6}, {3, 7, 6}, {1, 7, 5}, {3, 7, 5}.
Hence the maximum product of bitonic subsequence is 3*7*6 = 126
Input: arr[] = {1, 8, 3, 7}
Output: 56
Explanation:
Bitonic subsequences of size 3 are
{1, 8, 3}, {1, 8, 7}, {1, 7, 3}.
Hence the maximum product of bitonic subsequence is 1*8*7 = 56
天真的方法:一个简单的解决方案是找到所有大小为3的双子序列的乘积,并取其中的最大值。
算法:
- 将ans初始化为-1,这样,如果没有这样的子序列,则输出将为-1。
- 使用三个嵌套循环(循环变量为i,j和k)遍历数组,以选择数组的三个元素。
- 检查arr [j]> arr [i]和arr [j]> arr [k],然后用ans和arr [i] * arr [j] * arr [k]之间的最大值更新ans 。
下面是上述方法的实现:
C++
// C++ implemenation to find the
// maximum product of the bitonic
// subsequence of size 3
#include
using namespace std;
// Function to find the maximum
// product of bitonic subsequence
// of size 3
int maxProduct(int arr[], int n){
// Intialize ans to -1 if no such
// subsequence exist in the array
int ans = -1;
// Nested loops to choose the three
// elements of the array
for (int i = 0; i < n - 2; i++) {
for (int j = i + 1; j < n - 1; j++) {
for (int k = j + 1; k < n; k++) {
// Condition to check if
// they form a bitonic subsequence
if (arr[i] < arr[j] &&
arr[j] > arr[k])
ans = max(
ans, arr[i] * arr[j] * arr[k]
);
}
}
}
return ans;
}
// Driver Code
int main()
{
int arr[] = { 1, 8, 3, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function call
cout << maxProduct(arr, n) << endl;
}
Java
// Java implemenation to find the
// maximum product of the bitonic
// subsequence of size 3
import java.util.*;
class GFG{
// Function to find the maximum
// product of bitonic subsequence
// of size 3
static int maxProduct(int arr[], int n){
// Intialize ans to -1 if no such
// subsequence exist in the array
int ans = -1;
// Nested loops to choose the three
// elements of the array
for (int i = 0; i < n - 2; i++) {
for (int j = i + 1; j < n - 1; j++) {
for (int k = j + 1; k < n; k++) {
// Condition to check if
// they form a bitonic subsequence
if (arr[i] < arr[j] &&
arr[j] > arr[k])
ans = Math.max(
ans, arr[i] * arr[j] * arr[k]
);
}
}
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 8, 3, 7 };
int n = arr.length;
// Function call
System.out.print(maxProduct(arr, n) +"\n");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implemenation to find the
# maximum product of the bitonic
# subsequence of size 3
# Function to find the maximum
# product of bitonic subsequence
# of size 3
def maxProduct(arr, n):
# Intialize ans to -1 if no such
# subsequence exist in the array
ans = -1
# Nested loops to choose the three
# elements of the array
for i in range(n - 2):
for j in range(i + 1, n - 1):
for k in range(j + 1, n):
# Condition to check if
# they form a bitonic subsequence
if (arr[i] < arr[j] and arr[j] > arr[k]):
ans = max(ans, arr[i] * arr[j] * arr[k])
return ans
# Driver Code
if __name__ == '__main__':
arr= [ 1, 8, 3, 7]
n = len(arr)
# Function call
print(maxProduct(arr, n))
# This code is contributed by mohit kumar 29
C#
// C# implemenation to find the
// maximum product of the bitonic
// subsequence of size 3
using System;
class GFG {
// Function to find the maximum
// product of bitonic subsequence
// of size 3
static int maxProduct(int[] arr, int n)
{
// Intialize ans to -1 if no such
// subsequence exist in the array
int ans = -1;
// Nested loops to choose the three
// elements of the array
for (int i = 0; i < n - 2; i++) {
for (int j = i + 1; j < n - 1; j++) {
for (int k = j + 1; k < n; k++) {
// Condition to check if
// they form a bitonic subsequence
if (arr[i] < arr[j] &&
arr[j] > arr[k])
ans = Math.Max(ans, arr[i] * arr[j] * arr[k]
);
}
}
}
return ans;
}
// Driver code
static void Main()
{
int[] arr = new int[] { 1, 8, 3, 7 };
int n = arr.Length;
// Function call to find product
Console.Write(maxProduct(arr, n));
}
}
// This code is contributed by shubhamsingh
Javascript
C++
// C++ implemenation to find the
// maximum product of the bitonic
// subsequence of size 3
#include
using namespace std;
// Function to find the maximum
// product of bitonic subsequence
// of size 3
int maxProduct(int arr[], int n){
// Self Balancing BST
set s;
set::iterator it;
// Left array to store the
// maximum smallest value for
// every element in left of it
int Left[n];
// Right array to store the
// maximum smallest value for
// every element in right of it
int Right[n];
// Loop to find the maximum
// smallest element in left of
// every element in array
for (int i = 0; i < n; i++) {
s.insert(arr[i]);
it = s.lower_bound(arr[i]);
// Condition to check if there
// is a maximum smallest element
if (it != s.begin()) {
it--;
Left[i] = *it;
}
else {
Left[i] = -1;
}
}
// Clear Set
s.clear();
// Loop to find the maximum
// smallest element in right of
// every element in array
for (int i = n - 1; i >= 0; i--) {
s.insert(arr[i]);
it = s.lower_bound(arr[i]);
// Condition to check if there
// is such element exists
if (it != s.begin()) {
it--;
Right[i] = *it;
}
// If no such element exists.
else {
Right[i] = -1;
}
}
int ans = -1;
// Loop to find the maximum product
// bitonic subsequence of size 3
for (int i = 0; i < n; i++) {
if (Left[i] > 0 and Right[i] > 0)
ans = max(ans, arr[i] * Left[i] * Right[i]);
}
if (ans < 0) {
return -1;
}
else {
return ans;
}
}
// Driver Code
int main()
{
int arr[] = { 1, 8, 3, 7, 5, 6, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function Call
cout << maxProduct(arr, n);
}
Java
// Java implemenation to find the
// maximum product of the bitonic
// subsequence of size 3
import java.util.*;
import java.lang.System;
class GFG{
public static int maxProduct(int arr[],int n)
{
// Self Balancing BST
TreeSet ts = new TreeSet();
// Left array to store the
// maximum smallest value for
// every element in left of it
int Left[] = new int[n];
// Right array to store the
// maximum smallest value for
// every element in right of it
int Right[] = new int[n];
// Loop to find the maximum
// smallest element in left of
// every element in array
for(int i = 0; i < n; i++)
{
ts.add(arr[i]);
if(ts.lower(arr[i]) == null)
Left[i] = -1;
else
Left[i] = ts.lower(arr[i]);
}
ts.clear();
// Loop to find the maximum
// smallest element in right of
// every element in array
for (int i = n-1; i >= 0; i--)
{
ts.add(arr[i]);
if(ts.lower(arr[i]) == null)
Right[i] = -1;
else
Right[i] = ts.lower(arr[i]);
}
// Loop to find the maximum product
// bitonic subsequence of size 3
int ans = 0;
for(int i = 0; i < n; i++)
{
//Condition to check whether a sequence is bitonic or not
if(Left[i] != -1 && Right[i] != -1)
ans = Math.max(ans, Left[i] * arr[i] * Right[i]);
}
return ans;
}
// Driver Code
public static void main(String args[])
{
int arr[] = {1, 8, 3, 7, 5, 6, 7 };
int n = arr.length;
int maximum_product = maxProduct(arr,n);
System.out.println(maximum_product);
}
}
// This code is contributed by Siddhi.
56
性能分析:
- 时间复杂度:与上述方法一样,存在三个嵌套循环以找到大小为3的双子序列的最大乘积,因此时间复杂度为O(N 3 ) 。
- 辅助空间:与上述方法一样,没有使用额外的空间,因此辅助空间将为O(1) 。
高效的方法:想法是找到每个索引左侧和右侧的最大值,该最大值小于当前索引中存在的元素,为此,请使用自平衡BST,然后为每个元素找到最大乘积可以形成并最大程度地利用这些产品。
自平衡BST的实现与C++中的设置和Java的TreeSet中的设置相同。
算法:
- 声明一个自我平衡BST(比如S)。
- 声明两个新数组left []和right [] ,将arr [i]的下限存储在left [i]的该元素的左侧,并将arr [i]的下限存储在right [i]的该元素的右侧。
- 运行从0到长度– 1的循环,在该元素的左侧找到arr [i]的下限,并将其存储在left [i]中。
- 运行一个从长度-1到0的循环,以在该元素的右边找到arr [i]的下限并将其存储在right [i]中。
- 运行一个从0到长度– 1的循环,找到可以使用该元素形成的双子序列,以使用left []和right []数组获得最大乘积。也就是说,对于每个元素,可以形成的最大乘积双子序列为left [i] * right [i] * arr [i] 。
下面是上述方法的实现:
C++
// C++ implemenation to find the
// maximum product of the bitonic
// subsequence of size 3
#include
using namespace std;
// Function to find the maximum
// product of bitonic subsequence
// of size 3
int maxProduct(int arr[], int n){
// Self Balancing BST
set s;
set::iterator it;
// Left array to store the
// maximum smallest value for
// every element in left of it
int Left[n];
// Right array to store the
// maximum smallest value for
// every element in right of it
int Right[n];
// Loop to find the maximum
// smallest element in left of
// every element in array
for (int i = 0; i < n; i++) {
s.insert(arr[i]);
it = s.lower_bound(arr[i]);
// Condition to check if there
// is a maximum smallest element
if (it != s.begin()) {
it--;
Left[i] = *it;
}
else {
Left[i] = -1;
}
}
// Clear Set
s.clear();
// Loop to find the maximum
// smallest element in right of
// every element in array
for (int i = n - 1; i >= 0; i--) {
s.insert(arr[i]);
it = s.lower_bound(arr[i]);
// Condition to check if there
// is such element exists
if (it != s.begin()) {
it--;
Right[i] = *it;
}
// If no such element exists.
else {
Right[i] = -1;
}
}
int ans = -1;
// Loop to find the maximum product
// bitonic subsequence of size 3
for (int i = 0; i < n; i++) {
if (Left[i] > 0 and Right[i] > 0)
ans = max(ans, arr[i] * Left[i] * Right[i]);
}
if (ans < 0) {
return -1;
}
else {
return ans;
}
}
// Driver Code
int main()
{
int arr[] = { 1, 8, 3, 7, 5, 6, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function Call
cout << maxProduct(arr, n);
}
Java
// Java implemenation to find the
// maximum product of the bitonic
// subsequence of size 3
import java.util.*;
import java.lang.System;
class GFG{
public static int maxProduct(int arr[],int n)
{
// Self Balancing BST
TreeSet ts = new TreeSet();
// Left array to store the
// maximum smallest value for
// every element in left of it
int Left[] = new int[n];
// Right array to store the
// maximum smallest value for
// every element in right of it
int Right[] = new int[n];
// Loop to find the maximum
// smallest element in left of
// every element in array
for(int i = 0; i < n; i++)
{
ts.add(arr[i]);
if(ts.lower(arr[i]) == null)
Left[i] = -1;
else
Left[i] = ts.lower(arr[i]);
}
ts.clear();
// Loop to find the maximum
// smallest element in right of
// every element in array
for (int i = n-1; i >= 0; i--)
{
ts.add(arr[i]);
if(ts.lower(arr[i]) == null)
Right[i] = -1;
else
Right[i] = ts.lower(arr[i]);
}
// Loop to find the maximum product
// bitonic subsequence of size 3
int ans = 0;
for(int i = 0; i < n; i++)
{
//Condition to check whether a sequence is bitonic or not
if(Left[i] != -1 && Right[i] != -1)
ans = Math.max(ans, Left[i] * arr[i] * Right[i]);
}
return ans;
}
// Driver Code
public static void main(String args[])
{
int arr[] = {1, 8, 3, 7, 5, 6, 7 };
int n = arr.length;
int maximum_product = maxProduct(arr,n);
System.out.println(maximum_product);
}
}
// This code is contributed by Siddhi.
126
性能分析:
- 时间复杂度: O(NlogN)。
- 辅助空间: O(N)。
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。