Integer Break

Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.

For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4).

Note: you may assume that n is not less than 2.

Hint:

  1. There is a simple O(n) solution to this problem.
  2. You may check the breaking results of n ranging from 7 to 10 to discover the regularities.

From: http://www.cnblogs.com/grandyang/p/5411919.html

这道题给了我们一个正整数n,让我们拆分成至少两个正整数之和,使其乘积最大,题目提示中让我们用O(n)来解题,而且告诉我们找7到10之间的规律,那么我们一点一点的来分析:

正整数从1开始,但是1不能拆分成两个正整数之和,所以不能当输出。

那么2只能拆成1+1,所以乘积也为1。

数字3可以拆分成2+1或1+1+1,显然第一种拆分方法乘积大为2。

数字4拆成2+2,乘积最大,为4。

数字5拆成3+2,乘积最大,为6。

数字6拆成3+3,乘积最大,为9。

数字7拆为3+4,乘积最大,为12。

数字8拆为3+3+2,乘积最大,为18。

数字9拆为3+3+3,乘积最大,为27。

数字10拆为3+3+4,乘积最大,为36。

....

那么通过观察上面的规律,我们可以看出从5开始,数字都需要先拆出所有的3,一直拆到剩下一个数为2或者4,因为剩4就不用再拆了,拆成两个2和不拆没有意义,而且4不能拆出一个3剩一个1,这样会比拆成2+2的乘积小。那么这样我们就可以写代码了,先预处理n为2和3的情况,然后先将结果res初始化为1,然后当n大于4开始循环,我们结果自乘3,n自减3,根据之前的分析,当跳出循环时,n只能是2或者4,再乘以res返回即可:

 1 public class Solution {
 2     public int integerBreak(int n) {
 3         if (n == 2 || n == 3) return n - 1;
 4         int res = 1;
 5         while (n > 4) {
 6             res *= 3;
 7             n -= 3;
 8         }
 9         return res * n;
10     }
11 }

我们再来观察上面列出的10之前数字的规律,我们还可以发现数字7拆分结果是数字4的三倍,而7比4正好大三,数字8拆分结果是数字5的三倍,而8比5大3,后面都是这样的规律,那么我们可以把数字6之前的拆分结果都列举出来,然后之后的数通过查表都能计算出来,参见代码如下:

 1 public class Solution {
 2     int integerBreak(int n) {
 3         ArrayList<Integer> dp = new ArrayList<Integer>();
 4         dp.add(0);dp.add(0);dp.add(1);dp.add(2);dp.add(4);dp.add(6);dp.add(9);
 5         for (int i = 7; i <= n; ++i) {
 6             dp.add(3 * dp.get(i - 3));
 7         }
 8         return dp.get(n);
 9     }
10 }

 

转载于:https://www.cnblogs.com/beiyeqingteng/p/5727648.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值