Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

A typical CS style DP based solution:

class Solution(object):
    def __init__(self):
        self.hm = {}

    def integerBreak(self, n):
        if n in self.hm:
            return self.hm[n]
        ret = 0            
        for i in range(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] = ret            
        return ret

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 that 3 * (p - 3) > p, which means breaking it into two integers 3 and p - 3 makes the product larger while keeping the sum unchanged"

posted on 2016-04-20 07:15  Tonix  阅读(119)  评论(0编辑  收藏  举报