dp数组要n+1位,建立dp数组的过程中存在遍历拆分n的问题双重循环
class Solution {
public:
int integerBreak(int n
) {
vector
<int> dp(n
+1,0);
dp
[2]=1;
for(int i
=3;i
<=n
;i
++) {
for(int j
=1;j
<i
;j
++) {
dp
[i
]=max(dp
[i
],max(j
,dp
[j
])*max(i
-j
,dp
[i
-j
]));
}
}
return dp
[n
];
}
};
转载请注明原文地址:https://ipadbbs.8miu.com/read-23684.html