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. 深入浅出KNN算法

    概述 K最近邻(kNN,k-NearestNeighbor)分类算法 所谓K最近邻,就是k个最近的邻居的意思,说的是每个样本都可以用它最接近的k个邻居来代表. kNN算法的核心思想是如果一个样本在特征 ...

  2. python EasyUI + Django--整合 CSRF 防护去除

    先来张完整图: 关于Django 得CSRF  中间件      防护   GET 是不做CSRF验证得   但POST 默认验证  $.cookie('csrftoken'))    "v ...

  3. CodeForces - 402B Trees in a Row (暴力)

    题意:给定n个数,要求修改其中最少的数,使得这n个数满足ai + 1 - ai = k. 分析: 暴力,1000*1000. 1.这n个数,就是一个首项为a1,公差为k的等差数列.k已知,如果确定了a ...

  4. HDU - 1200 To and Fro

    题意:给定一个,其实是由一个图按蛇形输出而成的字符串,要求按从左到右,从上到下的顺序输出这个图. 分析: 1.把字符串转化成图 2.按要求输出图= = #include<cstdio> # ...

  5. BZOJ 5059 前鬼后鬼的守护

    题解: 解法一:用函数斜率什么的,不会,留坑 解法二: 某一个序列都变成一个值那么中位数最优 加入一个元素,与前面那一段区间的中位数比较 x>=mid什么事也不做 x<mid合并两端区间 ...

  6. html 鼠标样式 鼠标悬停 小手样式

    在style中添加cursor:pointer 实现鼠标悬停变成小手样式 先来一个示例 <div style="float:right"> <a class=&q ...

  7. Neo4j--常用的查询语句

    参考 https://www.w3cschool.cn/neo4j 准备工作 插入一堆朝代节点 插入我大明皇帝节点 创建大明皇帝统治大明王朝的关系 看一下结果 WHERE WHERE 语法 WHERE ...

  8. HTTP协议(四):首部

    前言 作者说:上一节中介绍了HTTP报文中的状态码,这一节同样是对报文的补充,介绍的是HTTP首部字段.不过,你如果是第一次见到这个东西,肯定会特别疑惑,什么是HTTP首部? <图解HTTP&g ...

  9. 19 01 03 css 中 reset 模块 设置

    主要就是让到时候 打入代码时候  把一些bug去除   或者 让一些固有的格式取消 /* 将标签默认的间距设为0 */ body,p,h1,h2,h3,h4,h5,h6,ul,dl,dt,form,i ...

  10. 64bit win7+VS2013+opencv2.4.9配置

    我的配置是opencv2.4.9与VS2013,在win7 64bit下. 从opencv官网(http://opencv.org/downloads.html),下载安装文件,然后双击安装包,类似于 ...