题目:
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).
思路:
令res[n]为n对应的最大积。那么递推方程就是:res[n]=max(i*res[n-i],i*(n-i))(其中i从1到n-1)。
程序:
class Solution {
public:int integerBreak(int n) {vector<int> res(n + 1,1);for(int i = 2;i <= n;i++){for(int j = 1;j < i;j++){int r = max(j * res[i - j],j * (i - j));res[i] = max(res[i],r);}}return res[n];}
};
本文发布于:2024-02-03 00:04:52,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170688989347343.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |