lc 343 Integer Break


343 Integer Break

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.

DP Accepted

dp[i]代表n为i时,和之积的最大值,所以dp[n]即为答案。动态转移方程为dp[i] = max(j*dp[i-j],max(dp[i],j*(i-j))),对于i之前的每一个j都遍历,用j来确定是否就此划分,如果不划分则是dp[i],如果划分则是j*dp[i-j]或j*(i-j),三者取最大值即可。

class Solution {
public:
int integerBreak(int n) {
vector<int> dp(n+1, 1);
for (int i = 3; i <= n; i++) {
for (int j = 1; j < i; j++) {
dp[i] = max(j*dp[i-j],max(dp[i],j*(i-j)));
}
}
return dp[n];
}
};

LN : leetcode 343 Integer Break的更多相关文章

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

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

  2. 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 ...

  3. leetcode 343. Integer Break(dp或数学推导)

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

  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. #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 ...

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

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

  7. LeetCode题解 343.Integer Break

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

  8. (dp)343. Integer Break

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

  9. 343. Integer Break

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

随机推荐

  1. request.getAttribute()与request.setAttribute()

    request.getAttribute()与request.setAttribute() request.getAttribute("nameOfObj")可得到JSP页面一表单 ...

  2. LuaInterface简单介绍

    LuaInterface简单介绍 Lua是一种非常好的扩展性语言.Lua解释器被设计成一个非常easy嵌入到宿主程序的库.LuaInterface则用于实现Lua和CLR的混合编程. (一)Lua f ...

  3. (WPF)附加属性

    <Window x:Class="DeepXAML.MainWindow" xmlns="http://schemas.microsoft.com/winfx/20 ...

  4. leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索II) 解题思路和方法

    Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...

  5. 2016/2/24 1,css有几种引入方式 2,div除了可以声明id来控制,还可以声明什么控制? 3,如何让2个div,并排显示。4,清除浮动 clear:left / right / both

    1,css有几种引入方式 使用HTML标签的STYLE属性 将STYLE属性直接加在单个的HTML元素标签上,控制HTML标签的表现样式.这种引入CSS的方式是分散灵活方便,但缺乏整体性和规划性,不利 ...

  6. SSH三大框架整合配置详细步骤(1)

    配置Struts2.0 3.1 基础配置 1)引入Struts必需的五个jar包.下载struts-2.1.6-all.zip解压后,struts-2.1.6\lib目录下是struts所有的相关ja ...

  7. ABAP WEBRFC

    通过WEBRFC实现在网页下载SMW0上传的文件 FUNCTION zhr_download_test. *"---------------------------------------- ...

  8. android stdio 异常

    1.android studio gradle project sync failed File -> Settings 搜索Gradle 2.eqmu-system-i386未响应   分辨率 ...

  9. YTU 2844: 改错题A-看电影

    2844: 改错题A-看电影 时间限制: 1 Sec  内存限制: 128 MB 提交: 69  解决: 47 题目描述 注:本题只需要提交标记为修改部分之间的代码,请按照C++方式提交. 小平家长为 ...

  10. 如何在Android studio中同时打开多个工程?

    最近学习Android Studio,想同时打开两个Project.但是点击File->Open之后,原有的Project被关闭掉了.怎么在新的窗口中打开Project呢? 解决: 点击Help ...