第十六周LeetCode
来源:互联网 发布:能在淘宝上发布 植树 编辑:程序博客网 时间:2024/06/05 06:11
题目
Integer Break
难度 Medium
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.
实现思路
可以用动态规划实现。用max_i[i]表示和为数字i的最大的乘积,那么max_[i]取决于和为i的组合j,i-j,max_i[j]和max_i[i-j],max_i[i]应为max_i[j]和j的最大值和max_i[i-j],j的最大值的乘积,即
max_i[i]=max(max_i[j],j)*max(max_i[i-j],j)。
实现代码
class Solution {public: int integerBreak(int n) { vector<int> max_i(n+1,0); max_i[1] = 1; for (int i = 2; i <= n; i++) { for (int j = 1; j <= i/2; j++) { max_i[i] = max(max_i[i],max(j,max_i[j])*max(i-j,max_i[i-j])); } } return max_i[n]; }};
阅读全文
0 0
- leetcode-第十六周
- 第十六周leetcode
- 第十六周LeetCode
- 第十六周LeetCode
- 第十六周LeetCode
- LeetCode题解 第十六周
- leetcode 第十六周解题报告
- 第十六周LeetCode算法题两道
- 第十六周:[leetCode] 79. Word Search
- leetcode【第十六周】左叶子节点求和
- leetcode第十六周解题总结--图
- 第十六周
- 第十六周
- 第十六周
- 第十六周
- 第十六周
- 第十六周
- 第十六周
- ACE proactor example
- Maven学习笔记(三)——Maven仓库(Repositories)、基础命令(二)&自动构建项目
- 算法——贪心法
- etcd-v2第一集
- 科技论文
- 第十六周LeetCode
- linux 中禁用USB存储
- 46 Three.js使用THREE.ExtrudeGeometry拉伸SVG为三维图形
- 十周第三次课 2017.12.20 Apache用户认证、域名跳转、Apache访问日志
- p7zip 在Mac下的应用-以在sublime text3配置Java开发环境
- ACE_Proactor UDP V2.0
- Win端搭建基于ncnn框架的MTCNN人脸检测
- 171220 逆向-i春秋【***】-******
- Logistic Regression with a Neural Network mindset v4 课程一第二周编程作业