343. Integer Break, 343 integerbreak

Source: Internet
Author: User

343. Integer Break, 343 integerbreak

Given a positive integerN, 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, givenN= 2, return 1 (2 = 1 + 1); givenN= 10, return 36 (10 = 3 + 3 + 4 ).

Note: you may assume thatNIs not less than 2.

Idea: Except for 2 and 3, the final result is the product of 2 and 3, with more than 3 and less than 2.

The Code is as follows:

 1 public class Solution { 2     public int integerBreak(int n) { 3          4         if(n==2) 5         return 1; 6         if(n==3) 7         return 2; 8          9         int result=1;10         if(n%3==2)11         {result=2;12             n=n-2;13         }14         else if(n%3==1)15         {16             result=4;17             n=n-4;18         }19         int count=n/3;20         while(count>0)21         {22             result=result*3;23             count--;24         }25         return result;26     }27 }

 

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.