Max Product Of Cutting Rope

2018-05-24  本文已影响0人  GakkiLove

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
上一篇下一篇

猜你喜欢

热点阅读