LeetCode343
来源:互联网 发布:socket 的类型 知乎 编辑:程序博客网 时间:2024/05/19 19:59
EX.343
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 and not larger than 58.
Solution:
题意分析,始终返回和为给定值的最大乘积
假定给定值为n,最大乘积为max
n = 2, max = 1*1;
n = 3, max = 2*1;
n = 4, max = 2*2;
n = 5, max = 3*2;
n = 6, max = 3*3;
n = 7, max = 3*4;
n = 8, max = 3*3*2;
n = 9, max = 3*3*3;
n = 10, max = 3*3*4;
……
max = 3*3*3…….*2 || 3*3*3……*3 || 3*3*3……*4
class Solution {public: int integerBreak(int n) { switch(n) { case 2: return 1; break; case 3: return 2; break; case 4: return 4; break; } int count = n/3; int temp = n%3; if (temp == 1) { return 4*getNum(count-1); } else if(temp == 0) { return getNum(count); } else { return 2*getNum(count); } } int getNum(int count) { int res = 1; for(int i = 0; i < count; i++) { res *= 3; } return res; }};
阅读全文
0 0
- LeetCode343
- leetcode343 Integer Break java
- LeetCode343. Integer Break
- [LeetCode343]Integer Break
- LeetCode343 Integer Break
- leetcode322 coin change & leetcode343 integer break
- leetcode343-Integer Break(整数拆分求最大乘积)
- Valid Number
- Shiro HelloWord
- SQL的四种连接-左外连接、右外连接、内连接、全连接
- 国内大公司的开源项目一览表
- 电子日历
- LeetCode343
- RedHat Linux下以yum方式安装Mysql5.6,修改默认的安装路径,以及完全卸载Mysql
- 机器学习基石-02-2-PLA何时停下来?
- PHP函数之error_reporting(E_ALL ^ E_NOTICE)详细说明
- Nginx简介及配置文件详解
- 總結——關於2017 10 13測試的分析總結
- Java注解
- IMX6Q Ft5216触屏驱动移植
- HDU-1754 I Hate It (线段树裸题 splay模板 单点修改 区间询问最大值)