首页 > 其他 > 详细

LeetCode "Integer Break"

时间:2016-04-20 08:13:22      阅读:273      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!