Leetcode 343. Integer Break
2016-05-11 11:54
225 查看
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.
分析前10个数
最大值 分解
2 2=1+1
2 3=1+2
4 4=2+2
6 5=2+3
9 6=3+3
12 7=3+4
18 8=3+3+2
27 9=3+3+3
36 10=3+3+4
经过分析可得当3越多,值则越大
代码如下:
public class Solution {
public int integerBreak(int n) {
if(n ==2){
return 1;
}if(n == 3){
return 2;
}
int num = 1;
while(n > 4){
num *= 3;
n -= 3;
}
return num*n;
}
}
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.
分析前10个数
最大值 分解
2 2=1+1
2 3=1+2
4 4=2+2
6 5=2+3
9 6=3+3
12 7=3+4
18 8=3+3+2
27 9=3+3+3
36 10=3+3+4
经过分析可得当3越多,值则越大
代码如下:
public class Solution {
public int integerBreak(int n) {
if(n ==2){
return 1;
}if(n == 3){
return 2;
}
int num = 1;
while(n > 4){
num *= 3;
n -= 3;
}
return num*n;
}
}
相关文章推荐
- flume高并发优化——(7)RandomAccessFile升级多文件source
- Maven web项目三种运行方式
- Exploratory Software Testing
- sed的多行模式模式
- 深入android Unable to resolve target 'android-XX'详解
- Spring MVC工作流程图
- 设置eclipse 黑色主题
- MAC软件-Photoshop2015破解安装
- java注解(二)Annotation使用案例
- 表哥的iOS 与 JS
- http://blog.163.com/eugeneheen_chen@126/blog/static/120812157201291994916866/
- Jquery 模态对话框 数据传递问题
- Python的functools模块
- 35.TextView排版参差不齐的问题
- 程序员的自我修养——读书笔记
- url scheme完全指南与app之间的调用(调用与被调用)
- 实例解析Java设计模式编程中的适配器模式使用
- Caffe 工程的一些编译错误以及解决方案
- python self introspection
- Caffe 工程的一些编译错误以及解决方案