Leetcode "Integer Break"

Source: Internet
Author: User

A typical CS style DP based solution:

classsolution (object):def __init__(self): self.hm= {}    defIntegerbreak (self, n):ifNinchself.hm:returnSelf.hm[n] ret=0 forIinchRange (1, N//2 + 1): v1=Self.integerbreak (i) V2= Self.integerbreak (N-i) RET= Max (ret, V1 * v2, V1 * (n-i), I * v2, I * (N-i)) Self.hm[n]=retreturnRet

But there ' s a Math based solution:

Https://leetcode.com/discuss/98249/easy-to-understand-c-with-explanation
In which: ' For any ' integer p strictly greater than 4 , it has the ' property ' such 3 * (p - 3) > p that, which means breaking it into, integers 3 and p - 3 makes the product larger while keeping the sum unchanged "

Leetcode "Integer Break"

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.