问题描述:

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.

解题思路:

给定一个正整数n(其实后来测试发现n到了59其结果就超出int的表示范围了),将其拆解成至少两个正整数的和,计算拆解数的乘积,找出所能获得的最大乘积。这种输入某个数求取某种最好解的题目最简单有效的方法就是多列几个例子看看是否有规律,对于这题我们先列出4到12的结果看看:

整数n 乘积最大对应的拆解(可能有多种) 最大乘积 模3
4 2 * 2 4 1
5 3 * 2 3*2 2
6 3 * 3 3^2 0
7 3 * 2 * 2 3*4 1
8 3 * 3 * 2 3^2*2 2
9 3 * 3 * 3 3^3 0
10 3 * 3 * 2 * 2 3^2*4 1
11 3 * 3 * 3 * 2 3^3*2 2
12 3 * 3 * 3 * 3 3^4 0

仔细观察拆解结果与n模3对应关系,发现余数为0时最大乘积为3的n/3次幂,余数为1时最大乘积为4乘以3的(n/3-1)次幂,余数为2时最大乘积为2乘以3的n/3次幂。

示例代码:

class Solution {
public:
int integerBreak(int n) {
if(n == 2){
return 1;
}
if(n == 3){
return 2;
} int k = n / 3;
int yu = n - k * 3;
int result = 0; if(yu == 1){
result = 4 * pow(3, k-1);
}
else if(yu == 2){
result = 2 * pow(3, k);
}
else{
result = pow(3, k);
}
return result;
}
};

343. Integer Break -- Avota的更多相关文章

  1. LN : leetcode 343 Integer Break

    lc 343 Integer Break 343 Integer Break Given a positive integer n, break it into the sum of at least ...

  2. #Week 11 - 343.Integer Break

    Week 11 - 343.Integer Break Given a positive integer n, break it into the sum of at least two positi ...

  3. 【LeetCode】343. Integer Break 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学解法 动态规划 日期 题目地址:https:// ...

  4. [LeetCode] 343. Integer Break 整数拆分

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  5. (dp)343. Integer Break

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  6. Leetcode 343. Integer Break

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  7. 343. Integer Break

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  8. leetcode@ [343] Integer Break (Math & Dynamic Programming)

    https://leetcode.com/problems/integer-break/ Given a positive integer n, break it into the sum of at ...

  9. LeetCode题解 343.Integer Break

    题目:Given a positive integer n, break it into the sum of at least two positive integers and maximize ...

随机推荐

  1. Linux操作系统的LILO详解

    LILO是一个在Linux环境编写的Boot Loader程序(所以安装和配置它都要在Linux下).它的主要功能就是引导Linux操作系统的启动.但是它不仅可以引导Linux,它还可以引导其他操作系 ...

  2. Dijkstra算法求解最短路径分析

    最短路径是图论算法中的经典问题.图分为有向图.无向图,路径权值有正值.负值,针对不同的情况需要分别选用不同的算法.在维基上面给出了各种不同的场景应用不同的算法的基本原则:最短路问题. 针对无向图,正权 ...

  3. 改变JVM中的参数以提高Eclipse的运行速度

    首先建立评估体系,将workspace里所有的项目close掉,关闭eclipse.优化的用例就是启动eclipse,open一个项目,eclipse会自动build这个项目,保证没有感觉到明显的卡, ...

  4. js两个时间比较

    var applyStart = $("#ApplyStart").val().replace(/-/g,'/'); var applyEnd = $("#ApplyEn ...

  5. 【转载】NativeSQL实例

    转自:http://blog.sina.com.cn/s/blog_3f2c03e301017fqz.html     ---------------------------------------- ...

  6. IOS关于XIB文件和调试时候显示不一样问题

    1 前言 今天工作中,遇到了一个xib文件布局问题,具体问题如下:在xib中加了一个图片,背景为已经切好的图片,但是当显示在模拟器上面的时候却显示不出来效果. 2 详述 2.1 问题截图      如 ...

  7. 违反并发性: UpdateCommand影响了预期 1 条记录中的 0 条 解决办法

    本文转载:http://www.cnblogs.com/litianfei/archive/2007/08/16/858866.html UpdateCommand和DeleteCommand出现DB ...

  8. 国外NET 空间免费申请使用

    最近研究微信公众帐号接口开发,需要使用到域名和空间,所以在度娘和谷哥的帮助下找到国外免费的空间, 刚刚注册完所以截图和大家分享下 注册地址:http://member.mywindowshosting ...

  9. Android SDK无法更新问题解决

    1.在SDK Manager下Tools->Options打开了SDK Manager的Settings,选中“Force https://… sources to be fetched usi ...

  10. 深入理解Fsync----JBD内核调试 专业打杂程序员 @github yy哥

    http://hustcat.github.io/ http://www.cnblogs.com/hustcat/p/3283955.html http://blog.sina.com.cn/s/ar ...