码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode "Integer Break"

时间:2016-04-20 08:14:32      阅读:387      评论:0      收藏:0      [点我收藏+]

标签:

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"

LeetCode "Integer Break"

标签:

原文地址:http://www.cnblogs.com/tonix/p/5411023.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!