Leetcode brush problem, summary, record, Memo 343

Source: Internet
Author: User

leetcode343Integer break

Given a positive integer n, break it into the sum of at least , positive integers and maximize the pro Duct of those integers. Return The maximum product you can get.

For example, given n = 2, return 1 (2 = 1 + 1); Given n = ten, return 36 (10 = 3 + 3 + 4).

Note: You may assume this n is not less than 2.

Show Hint

    Special thanks to @jianchao. Li.fighter for adding the problem and creating all test cases.

    Subscribe to see which companies asked this question

    using dynamic programming, we can find a rule by observing that the maximum value of n is either 1 on the basis of n-1, Either add 2 on top of the n-2. First, if it is based on the results of n-1, if you simply add a 1 in the n-1 array, the result is constant, so you need to add 1 to an element in the n-1 array, this 1 is best added to the smallest number in the n-1 array, so the product is the largest. If it is based on n-2, simply add a 2 to the array of n-2, and the one that evaluates to the maximum in these 2 arrays is the result of N. Suppose that the DP array is an array of each number that holds each number, such as dp[10] = {3, 3, 4}. The work to be done before the recursive formula, Dp[n-2].push_back (2); Sort (Dp[n-2].begin (), Dp[n-2].end ());  dp[n-1][0]++;  sort (Dp[n-1].begin (), Dp[n-1].end ()); Recursive formula: dp[n] = func (dp[n-2]) > func (dp[n-1])?  Dp[n-2]: dp[n-1]; Func is an auxiliary function that computes the result of an array and sorts it in order to serve the subsequent computations. You can view the code in detail.

    Class Solution {Public:int function (Vector<int> t) {int sum = 1;        for (int i = 0; i < t.size (); ++i) {sum *= t[i];    } return sum;        } int Integerbreak (int n) {if (n <= 0) {return 0;        } if (n = = 1) {return 1;                } vector<vector<int> > DP (n + 1, vector<int> ());        Dp[1].push_back (1);        Dp[2].push_back (1);                Dp[2].push_back (1);            for (int i = 3; I < dp.size (); ++i) {vector<int> temp1 = dp[i-2];            Temp1.push_back (2);            Vector<int> temp2 = dp[i-1];            temp2[0]++;            Sort (Temp1.begin (), Temp1.end ());                        Sort (Temp2.begin (), Temp2.end ());            if (function (TEMP1) > function (TEMP2)) {dp[i] = Temp1; } else {DP[I] = TEMP2;            }} return function (Dp[n]); }};

    Leetcode brush problem, summary, record, Memo 343

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.