Leetcode343-integer Break (integer split for maximum multiplication)

Source: Internet
Author: User
Tags split

Problem Description:

Given a positive integer n, break it into the sum of at least, positive integers and maximize the product of those inte Gers. 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, N is not less than 2.

Hint:
1) There is a simple O (n) solution to this problem.
2) You may check the breaking results for n ranging from 7 to discover the regularities.

A non-negative integer n is split into the sum of at least two integers, and the maximum value of the product of each number of the partitions is obtained.

Problem Solving:

class Solution {public:int integerbreak (int n) {if (n < 4) return n-1;
        int res = 1;
            while (n > 2) {//See n contains number of 3, multiply them until n<=2 res *= 3;
        N-= 3;
        if (n = = 0) return res;//n can be divisible 3,res is the individual 3 multiplied if (n = = 1) return (RES/3) * 4;//In addition to 3 1, one of 3 plus 1 into 4 and multiply if (n = = 2) return res * 2;//except 3 + 2, you can directly multiply 2 with res}}; 

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.