LeetCode——Integer Break
Question
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.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
Solution
动态规划,自底向上求解可以划分方式的最大值,注意要考虑不划分的情况,所以要取两者的最大值。
Code
class Solution {
public:
int integerBreak(int n) {
table.resize(60);
table[1] = 1;
for (int i = 2; i <= n; i++) {
table[i] = 1;
for (int j = 1; j <= i / 2; j++) {
// 要考虑不划分的情况
int tmp = max(j, table[j]) * max(i - j, table[i - j]);
if (tmp > table[i])
table[i] = tmp;
}
}
return table[n];
}
private:
vector<int> table;
};
LeetCode——Integer Break的更多相关文章
- [LeetCode]Integer Break(Dp或胡搞或推公式)
343. Integer Break Given a positive integer n, break it into the sum of at least two positive intege ...
- [LeetCode] Integer Break 整数拆分
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- LeetCode "Integer Break"
A typical CS style DP based solution: class Solution(object): def __init__(self): self.hm = {} def i ...
- 【LeetCode】343. Integer Break 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学解法 动态规划 日期 题目地址:https:// ...
- LeetCode 343. 整数拆分(Integer Break) 25
343. 整数拆分 343. Integer Break 题目描述 给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化. 返回你可以获得的最大乘积. 每日一算法2019/5/2 ...
- 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 ...
- #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 ...
- 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 ...
- leetcode 343. Integer Break(dp或数学推导)
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
随机推荐
- CLR via 笔记 5.3 值类型的装箱和拆箱
1.装箱 为了将一个值类型转换成一个引用类型,要使用一个名为装箱(Boxing)的机制. 1.在托管堆中分配好内存.分配的内存量是值类型的各个字段需要的内存量加上托管堆的所有对象都有的两个额外成员(类 ...
- Go学习笔记一:解析toml配置文件
本文系作者原创,转载请注明出处https://www.cnblogs.com/sonofelice/p/9085291.html . 一些mysql或者日志路径的信息需要放在配置文件中.那么本博文主要 ...
- Service Receiver Activity 之间的通信
一.Activity与Service 1. 通过Intent,例子如下: Intent intent = new Intent(this, Myservice.class); // intent .p ...
- (0.1)windows下的mysql配置使用步骤
目录 1.基于windows平台的mysql项目场景 2.mysql数据库运行环境准备 3.下载mysql 4.通过Installer方式(即msi方式)安装mysql 5.卸载mysql ————— ...
- CloudFoundry V2 单机版离线安装(伪离线安装)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/wangdk789/article/details/30255763 之前安装CloudFou ...
- 1.新建项目出现包名有一道红线The SDK platform-tools version ((23)) is too old to check APIs compiled with API 20
原因分析: 就是platform-tools的版本太低导致的 解决方法: 1.点开SDK Manager,打开SDK Tools面板,将Platform-tools更新 2.更新完之后重启as即可
- Oracle 性能调优 SQL_TRACE
思维导图 Oracle优化10-SQL_TRACE解读 Oracle优化11-10046事件 概述 当我们想了解一条SQL或者是PL/SQL包的运行情况时,特别是当他们的性能非常差时,比如有的时候看起 ...
- 使用stringstream格式化字符串
stringstream所在头文件为<sstream> 一般有如下常用功能: 1.安全格式化字符串 stringstream常用来安全的格式化若干个字符串,数值到一个缓冲区, 而不用担心溢 ...
- django【F和Q】
一.F 案例每人增加500工资 ORM:UserInfo.objects.filter().update(salary=500) 这不行吧 SQL: update userinfo set salar ...
- python全栈开发从入门到放弃之初识面向对象
面向过程 VS 面向对象 面向过程的程序设计的核心是过程(流水线式思维),过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西. 优点是:极大的降低了写程序的复 ...