[英]Break a positive integer into the sum of at least two positive integers and return the maximum product
所以这是最近的一个面试问题,给定一个正整数 n,将其分解为至少两个正整数的和,并最大化这些整数的乘积。 返回您可以获得的最大产品。
例如,给定 n = 2,返回 1 (2 = 1 + 1); 给定 n = 10,返回 36 (10 = 3 + 3 + 4)。
我正在尝试递归地解决它,该方法首先将数字分成两半并找到最大乘积并继续拆分每一半直到我们得到最大值。
这是我的代码,
现在我对如何终止递归的基本条件有点迷茫。 如果有人可以帮助我解决我的方法而不是提出完全不同的解决方案,我将不胜感激。"
我编写了一个简单的 Java 应用程序来计算数字 2 到 20 的整数之和的最大乘积。第一个数字是总和。 中间的数字是总和的因数。 最后的数字是因子的乘积。 这是结果。
2 [1, 1] 1
3 [2, 1] 2
4 [2, 2] 4
5 [3, 2] 6
6 [3, 3] 9
7 [4, 3] 12
8 [3, 3, 2] 18
9 [3, 3, 3] 27
10 [4, 3, 3] 36
11 [3, 3, 3, 2] 54
12 [3, 3, 3, 3] 81
13 [4, 3, 3, 3] 108
14 [3, 3, 3, 3, 2] 162
15 [3, 3, 3, 3, 3] 243
16 [4, 3, 3, 3, 3] 324
17 [3, 3, 3, 3, 3, 2] 486
18 [3, 3, 3, 3, 3, 3] 729
19 [4, 3, 3, 3, 3, 3] 972
20 [3, 3, 3, 3, 3, 3, 2] 1458
calculateMaximumFactors 方法计算具有最大乘积的因子。 因子方法生成总和的因子。 乘积方法计算因子的乘积。 这是代码:
package com.ggl.testing;
import java.util.Arrays;
public class MaximumProduct {
public static void main(String[] args) {
for (int sum = 2; sum <= 20; sum++) {
System.out.print(sum + " ");
System.out.println(calculateMaximumFactors(sum));
}
}
private static String calculateMaximumFactors(int sum) {
int[] previousFactors = new int[0];
int maxProduct = 0;
for (int i = 2; i <= sum; i++) {
int[] factors = factor(sum, i);
int product = product(factors);
if (product > maxProduct) {
maxProduct = product;
previousFactors = Arrays.copyOf(factors, factors.length);
}
}
return Arrays.toString(previousFactors) + " " + maxProduct;
}
private static int[] factor(int sum, int divisor) {
if (sum < divisor) {
return new int[0];
}
int num = sum / divisor;
int remainder = sum % divisor;
int[] result = new int[divisor];
for (int i = 0; i < divisor; i++) {
result[i] = num;
if (remainder > 0) {
result[i]++;
remainder--;
}
}
return result;
}
private static int product(int[] factors) {
int product = 1;
for (int i = 0; i < factors.length; i++) {
product *= factors[i];
}
return product;
}
}
这是我对问题的解决方案:(想法:将整数分解为 3 的倍数是最佳的)
public int integerBreak(int n) {
// base case :
if (n == 2 || n == 3){
return (n-1);
}
int maxProduct = 1;
while (n > 4){
n -= 3;
maxProduct *= 3; // Keep multiplying 3.
}
return (n * maxProduct); // multiply with left over n.
}
这是简单的 O(N) 解决方案。 希望这有助于某人!
这个想法是将数字分解为 2 或 3 的倍数。如果你写出像 7 到 10 这样的几个数字的分解结果,你应该明白这个想法。 假设最大数量为 60,有一个简单的动态解决方案:
int dp[60];
public:
int integerBreak(int n)
{
dp[1]=1,dp[2]=1,dp[3]=2,dp[4]=4,dp[5]=6,dp[6]=9;
for(int i=7;i<=n;i++)
dp[i]=max(dp[i-3]*3,dp[i-2]*2);
return dp[n];
}
};
正如我在上面的评论中所写,我们必须将数字分成 3 个。 如果我们推导出最大值,我们得到 e(对数的底)为 2 < e < 3。但事情是 6= 3 3 和 6=2<\/em> 2*2 所以每个 2 的三元组都可以替换为一个元组最大产品为 3。
所以这是我写的代码。 它是用 Python 编写的,所以我希望你不要介意 -
def maximize_product(num):
product = 1
if num == 2 or num == 3:
return num - 1
else:
while num > 4:
product = product * 3
num -= 3
return num * product
如果您进行循环尝试查找数字,则会变得复杂且效率低下(数字越大,您找到它所需的时间越长,您需要考虑索引等)
最好最快的算法是中点算法,即将给定的数除以2,如果数是奇数,则计算偏差,最后计算乘积
static int func(int number) {
int result = 0;
if (number < 0) {
System.err.println("no negative allowed");
System.exit(0);
}
int a = 0;
int b = 0;
a = number / 2;
b = number / 2;
a += number - (a + b);
result = a * b;
System.out.println(" this is a " + a);
System.out.println(" this is b " + b);
return result;
}
如果你像这样执行它
public static void main(String[] args) {
int number = 9;
int result = func(number);
System.out.println(result);
}
会得到正确的结果...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.