动态规划中级教程 343. Integer Break
来源:互联网 发布:mmd双人动作数据 编辑:程序博客网 时间:2024/06/16 11:54
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.
分析下题目
我们有两个手段
1.拆成两个数直接相乘
2.拆成n个数相乘(这里是我们动态规划的核心)我们把它优化成dp【i】*(i-j)
当拆成n个数的时候可以把其中一个数再拆成两个(造成重复子问题的关键)
dp【1】=1
dp【2】=1;
dp【3】=max(1*2,dp【2】*1)
dp【4】=max(2*2,dp【2】*2,dp【3】*1)
dp【5】=max(2*3,dp【2】*3,dp【3】*2,dp【4】*1)
dp【6】=max(3*3,dp【3】*3,。。。。。。。。。。。这里应该看出来了
可以写状态转移了
dp【i】=max{(i-j)*j, dp[i-j]*j }
class Solution {public: int integerBreak(int n) { int dp[n+1]; for(int i=0;i<=n;i++) { dp[i]=0; } dp[0]=0; dp[1]=1; dp[2]=1; for(int i=2;i<=n;i++) { for(int j=1;j<=i;j++) { dp[i]=max(dp[i],(i-j)*j); dp[i]=max(dp[i],dp[i-j]*j); } } return dp[n]; }};
阅读全文
0 0
- 动态规划中级教程 343. Integer Break
- 动态规划-343. Integer Break
- 343.[LeetCode] 动态规划 Integer Break
- 343. Integer Break(LeetCode, C++, 动态规划)
- 343. Integer Break (动态规划)
- 动态规划解343. Integer Break
- 343. Integer Break 类别:动态规划 难度:medium
- 动态规划——343. Integer Break[Medium]
- LeetCode 343. Integer Break 动态规划,状态转移方程
- 动态规划中级教程 322. Coin Change
- 动态规划中级教程 279. Perfect Squares
- 动态规划中级教程91. Decode Ways
- Leetcode 343(Integer Break非动态规划求解)
- 动态规划中级题解
- 动态规划中级教程(leetcode)152.Maximum Product Subarray
- 动态规划中级教程300.Longest Increasing Subsequence
- 动态规划中级教程。 650. 2 Keys Keyboard
- 动态规划中级教程 646. Maximum Length of Pair Chain
- 5.2.2—二叉树的构建—Construct Binary Tree from Inorder and Postorder Traversal
- 5.3.3—二叉查找树—Validate Binary Sear Tree
- [VC.NET]托管代码和非托管代码
- 5.3.4—二叉查找树—Convert Sorted Array to Binary Sear Tree
- 5.3.5—二叉查找树—Convert Sorted List to Binary Sear Tree
- 动态规划中级教程 343. Integer Break
- 大型复杂信息系统研发的大学式组织管理方法
- UVA11754 Code Feat
- 坦克大战学习笔记
- 5.4.1—二叉树的递归—Minimum Depth of Binary Tree
- opencv获取彩色(RGB)图像直方图
- Oracle SQL语句优化
- docker学习笔记--重基础使用
- Wireshark图解教程(简介、抓包、过滤器)