美文网首页
Max Product Of Cutting Rope

Max Product Of Cutting Rope

作者: GakkiLove | 来源:发表于2018-05-24 21:50 被阅读0次

    Given a rope with positive integer-length n, how to cut the rope into m integer-length parts with length p[0], p[1], ...,p[m-1], in order to get the maximal product of p[0]p[1] ... p[m-1]? m is determined by you and must be greater than 0 (at least one cut must be made). Return the max product you can have.

    Assumptions

    n >= 2

    Examples

    n = 12, the max product is 3 3 3 3 = 81(cut the rope into 4 pieces with length of each is 3).

    class Solution(object):
      def maxProduct(self, length):
        if length == 2:
          return 1
        max_val = 0
        for i in xrange(1,length-1):
          max_val = max(max_val,max(i * (length - i),self.maxProduct(length - i) * i))
        return max_val
    

    相关文章

      网友评论

          本文标题:Max Product Of Cutting Rope

          本文链接:https://www.haomeiwen.com/subject/gpjejftx.html