Leetcode343(力扣343):整数拆分

    技术2022-07-13  78

    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]; } };
    Processed: 0.009, SQL: 9