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

[Algo] 87. Max Product Of Cutting Rope

时间:2020-02-23 09:41:30      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:you   osi   term   The   ima   return   ons   least   into   

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]? 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).

public class Solution {
  public int maxProduct(int length) {
    // Write your solution here
    int[] arr = new int[length + 1];
    arr[0] = 0;
    for (int i = 0; i < arr.length; i++) {
      for (int j = 0; j < i; j++) {
        // arr[i] = Math.max(arr[i], Math.max(i, arr[j] * (i - j)));
        arr[i] = Math.max(arr[i], (i - j) * Math.max(j, arr[j]));
      }
    }
    return arr[length];
  }
}

 

[Algo] 87. Max Product Of Cutting Rope

标签:you   osi   term   The   ima   return   ons   least   into   

原文地址:https://www.cnblogs.com/xuanlu/p/12348426.html

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