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
if (length == 0 || length == 1) {
return 0;
}
int[] cutArr = new int[length + 1];
cutArr[1] = 0;
for (int i = 2; i <= length; i++) {
for (int j = 1; j < i; j++) {
int curMax = Math.max((i - j) * j, cutArr[i - j] * j);
cutArr[i] = Math.max(cutArr[i], curMax);
}
}
return cutArr[length];
}
}

DFS

public class Solution {
public int maxProduct(int length) {
// Write your solution here
if (length == 0 || length == 1) {
return 0;
}
int res = 0;
for (int i = 1; i < length; i++) {
int curMax = Math.max(i * (length - i), i * maxProduct(length - i));
res = Math.max(res, curMax);
}
return res;
}
}

[Algo] 87. Max Product Of Cutting Rope的更多相关文章

  1. LeetCode 笔记26 Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  2. LeetCode OJ 152. Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  3. LeetCode 152. Maximum Product Subarray (最大乘积子数组)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  4. [leetcode]152. Maximum Product Subarray最大乘积子数组

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  5. MDX Step by Step 读书笔记(七) - Performing Aggregation 聚合函数之 Max, Min, Count , DistinctCount 以及其它 TopCount, Generate

    MDX 中最大值和最小值 MDX 中最大值和最小值函数的语法和之前看到的 Sum 以及 Aggregate 等聚合函数基本上是一样的: Max( {Set} [, Expression]) Min( ...

  6. Maximum Product Subarray LT152

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  7. C#LeetCode刷题之#628-三个数的最大乘积( Maximum Product of Three Numbers)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3726 访问. 给定一个整型数组,在数组中找出由三个数组成的最大乘 ...

  8. 主成份分析PCA

    Data Mining 主成分分析PCA 降维的必要性 1.多重共线性--预测变量之间相互关联.多重共线性会导致解空间的不稳定,从而可能导致结果的不连贯. 2.高维空间本身具有稀疏性.一维正态分布有6 ...

  9. 主成分分析PCA(转载)

    主成分分析PCA 降维的必要性 1.多重共线性--预测变量之间相互关联.多重共线性会导致解空间的不稳定,从而可能导致结果的不连贯. 2.高维空间本身具有稀疏性.一维正态分布有68%的值落于正负标准差之 ...

随机推荐

  1. POJ 1330 LCA最近公共祖先 离线tarjan算法

    题意要求一棵树上,两个点的最近公共祖先 即LCA 现学了一下LCA-Tarjan算法,还挺好理解的,这是个离线的算法,先把询问存贮起来,在一遍dfs过程中,找到了对应的询问点,即可输出 原理用了并查集 ...

  2. GDI+3

    关于这个的例子其实网上已经有这方面的资料了,但是为了文章的完整性,还是觉得有必要讲解.我们先来看一下效果:     ( 图2 )接下来看看这是如何做到的. 思路:聊天窗体上有一个截图按钮,点击按钮后, ...

  3. 《thinkphp》二、创建实例

    1.目录结构 project 应用部署目录 ├─application 应用目录(可设置) │ ├─common 公共模块目录(可更改) │ ├─index 模块目录(可更改) │ │ ├─confi ...

  4. Spring 控制反转容器(Inversion of Control – IOC)

    系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of Contro ...

  5. case...when...和decode——oracle

    1.decode函数: 用法:decode(条件,值1,翻译1,值2,翻译2,......,缺省值): 例子: ','失败','未知') from table t1;--改变字段的显示值 ,变量1,变 ...

  6. vue的computed和method的区别

    (1)computed是响应式的,methods并非响应式. (2)computed是带缓存的 (3)computed中的成员可以只定义一个函数作为只读属性,也可以定义get/set变成可读写属性,这 ...

  7. windows操作

    5.windows激活 数字权利许可工具激活 https://jingyan.baidu.com/article/9113f81b4d49232b3314c75e.html 4.网络连接不上 原因,v ...

  8. typeof()与Object.prototype.toString.call()

    用typeof方法只能初步判断number string undefined boolean object function symbol这几种初步类型 使用Object.prototype.toSt ...

  9. POJ-3629 模拟

    A - Card Stacking Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  10. 201812-1 小明上学 Java

    思路: 上学这个题和放学有区别,上学是小明每到一个路口的情况,是实时更新的.不是只有出发时间,那样就比较复杂了. 这个题需要注意:黄灯之后要等红灯,想一下交通规则. import java.util. ...