给定长度为n米的绳索,以使所有部分的长度乘积最大的方式将绳索切成整数长度的不同部分。您必须至少裁切一次。假设绳子的长度超过2米。
例子:
Input: n = 2
Output: 1 (Maximum obtainable product is 1*1)
Input: n = 3
Output: 2 (Maximum obtainable product is 1*2)
Input: n = 4
Output: 4 (Maximum obtainable product is 2*2)
Input: n = 5
Output: 6 (Maximum obtainable product is 2*3)
Input: n = 10
Output: 36 (Maximum obtainable product is 3*3*4)
1)最佳子结构:
此问题类似于“杆切割问题”。我们可以通过在不同位置进行切割并比较切割后获得的值来获得最大乘积。我们可以为剪切后获得的片段递归调用相同的函数。
令maxProd(n)为长度为n的绳索的最大乘积。 maxProd(n)可以编写如下。
对于{1,2,3..n}中的所有i,maxProd(n)= max(i *(ni),maxProdRec(ni)* i)
2)重叠子问题
以下是问题的简单递归实现。该实现仅遵循上述递归结构。
C++
// A Naive Recursive method to find maxium product
#include
using namespace std;
// Utility function to get the maximum of two and three integers
int max(int a, int b) { return (a > b)? a : b;}
int max(int a, int b, int c) { return max(a, max(b, c));}
// The main function that returns maximum product obtainable
// from a rope of length n
int maxProd(int n)
{
// Base cases
if (n == 0 || n == 1) return 0;
// Make a cut at different places and take the maximum of all
int max_val = 0;
for (int i = 1; i < n; i++)
max_val = max(max_val, i*(n-i), maxProd(n-i)*i);
// Return the maximum of all values
return max_val;
}
/* Driver program to test above functions */
int main()
{
cout << "Maximum Product is " << maxProd(10);
return 0;
}
Java
// Java program to find maxium product
import java.io.*;
class GFG {
// The main function that returns
// maximum product obtainable from
// a rope of length n
static int maxProd(int n)
{
// Base cases
if (n == 0 || n == 1) return 0;
// Make a cut at different places
// and take the maximum of all
int max_val = 0;
for (int i = 1; i < n; i++)
max_val = Math.max(max_val,
Math.max(i * (n - i),
maxProd(n - i) * i));
// Return the maximum of all values
return max_val;
}
/* Driver program to test above functions */
public static void main(String[] args)
{
System.out.println("Maximum Product is "
+ maxProd(10));
}
}
// This code is contributed by Prerna Saini
Python3
# The main function that returns maximum
# product obtainable from a rope of length n
def maxProd(n):
# Base cases
if (n == 0 or n == 1):
return 0
# Make a cut at different places
# and take the maximum of all
max_val = 0
for i in range(1, n - 1):
max_val = max(max_val, max(i * (n - i), maxProd(n - i) * i))
#Return the maximum of all values
return max_val;
# Driver program to test above functions
print("Maximum Product is ", maxProd(10));
# This cide is contributed
# by Sumit Sudhakar
C#
// C# program to find maxium product
using System;
class GFG {
// The main function that returns
// the max possible product
static int maxProd(int n)
{
// n equals to 2 or 3 must
// be handled explicitly
if (n == 2 || n == 3)
return (n - 1);
// Keep removing parts of size
// 3 while n is greater than 4
int res = 1;
while (n > 4) {
n -= 3;
// Keep multiplying 3 to res
res *= 3;
}
// The last part multiplied
// by previous parts
return (n * res);
}
// Driver code
public static void Main()
{
Console.WriteLine("Maximum Product is "
+ maxProd(10));
}
}
// This code is contributed by Sam007
PHP
Javascript
C
// A Dynamic Programming solution for Max Product Problem
int maxProd(int n)
{
int val[n+1];
val[0] = val[1] = 0;
// Build the table val[] in bottom up manner and return
// the last entry from the table
for (int i = 1; i <= n; i++)
{
int max_val = 0;
for (int j = 1; j <= i/2; j++)
max_val = max(max_val, (i-j)*j, j*val[i-j]);
val[i] = max_val;
}
return val[n];
}
C++
#include
using namespace std;
/* The main function that teturns the max possible product */
int maxProd(int n)
{
// n equals to 2 or 3 must be handled explicitly
if (n == 2 || n == 3) return (n-1);
// Keep removing parts of size 3 while n is greater than 4
int res = 1;
while (n > 4)
{
n -= 3;
res *= 3; // Keep multiplying 3 to res
}
return (n * res); // The last part multiplied by previous parts
}
/* Driver program to test above functions */
int main()
{
cout << "Maximum Product is " << maxProd(10);
return 0;
}
Java
// Java program to find maximum product
import java.io.*;
class GFG {
/* The main function that returns the
max possible product */
static int maxProd(int n)
{
// n equals to 2 or 3 must be handled
// explicitly
if (n == 2 || n == 3) return (n-1);
// Keep removing parts of size 3
// while n is greater than 4
int res = 1;
while (n > 4)
{
n -= 3;
// Keep multiplying 3 to res
res *= 3;
}
// The last part multiplied by
// previous parts
return (n * res);
}
/* Driver program to test above functions */
public static void main(String[] args)
{
System.out.println("Maximum Product is "
+ maxProd(10));
}
}
// This code is contributed by Prerna Saini
Python3
# The main function that returns the
# max possible product
def maxProd(n):
# n equals to 2 or 3 must
# be handled explicitly
if (n == 2 or n == 3):
return (n - 1)
# Keep removing parts of size 3
# while n is greater than 4
res = 1
while (n > 4):
n -= 3;
# Keep multiplying 3 to res
res *= 3;
# The last part multiplied
# by previous parts
return (n * res)
# Driver program to test above functions
print("Maximum Product is ", maxProd(10));
# This code is contributed
# by Sumit Sudhakar
C#
// C# program to find maxium product
using System;
class GFG {
// The main function that returns
// maximum product obtainable from
// a rope of length n
static int maxProd(int n)
{
// Base cases
if (n == 0 || n == 1)
return 0;
// Make a cut at different places
// and take the maximum of all
int max_val = 0;
for (int i = 1; i < n; i++)
max_val = Math.Max(max_val,
Math.Max(i * (n - i),
maxProd(n - i) * i));
// Return the maximum of all values
return max_val;
}
// Driver code
public static void Main()
{
Console.WriteLine("Maximum Product is "
+ maxProd(10));
}
}
// This code is contributed by Sam007
PHP
4)
{
$n = $n - 3;
// Keep multiplying 3 to res
$res = $res * 3;
}
// The last part multiplied
// by previous parts
return ($n * $res);
}
// Driver code
echo ("Maximum Product is ");
echo(maxProd(10));
// This code is contributed
// by Shivi_Aggarwal
?>
Javascript
输出:
Maximum Product is 36
考虑到以上实现,下面是长度为5的Rope的递归树。
在上面的部分递归树中,mP(3)被求解两次。我们可以看到,有许多子问题一次又一次地得到解决。由于再次调用了相同的问题,因此此问题具有“重叠子问题”属性。因此,该问题具有动态编程问题的两个属性(请参阅此内容)。像其他典型的动态编程(DP)问题一样,可以通过以自下而上的方式构造临时数组val []来避免相同子问题的重新计算。
C
// A Dynamic Programming solution for Max Product Problem
int maxProd(int n)
{
int val[n+1];
val[0] = val[1] = 0;
// Build the table val[] in bottom up manner and return
// the last entry from the table
for (int i = 1; i <= n; i++)
{
int max_val = 0;
for (int j = 1; j <= i/2; j++)
max_val = max(max_val, (i-j)*j, j*val[i-j]);
val[i] = max_val;
}
return val[n];
}
动态编程解决方案的时间复杂度为O(n ^ 2),并且需要O(n)额外空间。
一个棘手的解决方案:
如果我们看到一些有关此问题的示例,则可以轻松观察以下模式。
通过重复切割尺寸大于4的尺寸为3的零件可以获得最大乘积,将最后一个零件的尺寸保留为2或3或4。例如,n = 10,则最大乘积为3、3, 4.对于n = 11,最大乘积可通过3、3、3、2获得。以下是该方法的实现。
C++
#include
using namespace std;
/* The main function that teturns the max possible product */
int maxProd(int n)
{
// n equals to 2 or 3 must be handled explicitly
if (n == 2 || n == 3) return (n-1);
// Keep removing parts of size 3 while n is greater than 4
int res = 1;
while (n > 4)
{
n -= 3;
res *= 3; // Keep multiplying 3 to res
}
return (n * res); // The last part multiplied by previous parts
}
/* Driver program to test above functions */
int main()
{
cout << "Maximum Product is " << maxProd(10);
return 0;
}
Java
// Java program to find maximum product
import java.io.*;
class GFG {
/* The main function that returns the
max possible product */
static int maxProd(int n)
{
// n equals to 2 or 3 must be handled
// explicitly
if (n == 2 || n == 3) return (n-1);
// Keep removing parts of size 3
// while n is greater than 4
int res = 1;
while (n > 4)
{
n -= 3;
// Keep multiplying 3 to res
res *= 3;
}
// The last part multiplied by
// previous parts
return (n * res);
}
/* Driver program to test above functions */
public static void main(String[] args)
{
System.out.println("Maximum Product is "
+ maxProd(10));
}
}
// This code is contributed by Prerna Saini
Python3
# The main function that returns the
# max possible product
def maxProd(n):
# n equals to 2 or 3 must
# be handled explicitly
if (n == 2 or n == 3):
return (n - 1)
# Keep removing parts of size 3
# while n is greater than 4
res = 1
while (n > 4):
n -= 3;
# Keep multiplying 3 to res
res *= 3;
# The last part multiplied
# by previous parts
return (n * res)
# Driver program to test above functions
print("Maximum Product is ", maxProd(10));
# This code is contributed
# by Sumit Sudhakar
C#
// C# program to find maxium product
using System;
class GFG {
// The main function that returns
// maximum product obtainable from
// a rope of length n
static int maxProd(int n)
{
// Base cases
if (n == 0 || n == 1)
return 0;
// Make a cut at different places
// and take the maximum of all
int max_val = 0;
for (int i = 1; i < n; i++)
max_val = Math.Max(max_val,
Math.Max(i * (n - i),
maxProd(n - i) * i));
// Return the maximum of all values
return max_val;
}
// Driver code
public static void Main()
{
Console.WriteLine("Maximum Product is "
+ maxProd(10));
}
}
// This code is contributed by Sam007
的PHP
4)
{
$n = $n - 3;
// Keep multiplying 3 to res
$res = $res * 3;
}
// The last part multiplied
// by previous parts
return ($n * $res);
}
// Driver code
echo ("Maximum Product is ");
echo(maxProd(10));
// This code is contributed
// by Shivi_Aggarwal
?>
Java脚本
输出:
Maximum Product is 36