[Algo] 87. Max Product Of Cutting Rope
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).
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的更多相关文章
- LeetCode 笔记26 Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode OJ 152. Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode 152. Maximum Product Subarray (最大乘积子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode]152. Maximum Product Subarray最大乘积子数组
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- MDX Step by Step 读书笔记(七) - Performing Aggregation 聚合函数之 Max, Min, Count , DistinctCount 以及其它 TopCount, Generate
MDX 中最大值和最小值 MDX 中最大值和最小值函数的语法和之前看到的 Sum 以及 Aggregate 等聚合函数基本上是一样的: Max( {Set} [, Expression]) Min( ...
- Maximum Product Subarray LT152
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- C#LeetCode刷题之#628-三个数的最大乘积( Maximum Product of Three Numbers)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3726 访问. 给定一个整型数组,在数组中找出由三个数组成的最大乘 ...
- 主成份分析PCA
Data Mining 主成分分析PCA 降维的必要性 1.多重共线性--预测变量之间相互关联.多重共线性会导致解空间的不稳定,从而可能导致结果的不连贯. 2.高维空间本身具有稀疏性.一维正态分布有6 ...
- 主成分分析PCA(转载)
主成分分析PCA 降维的必要性 1.多重共线性--预测变量之间相互关联.多重共线性会导致解空间的不稳定,从而可能导致结果的不连贯. 2.高维空间本身具有稀疏性.一维正态分布有68%的值落于正负标准差之 ...
随机推荐
- jQuery获取display为none的隐藏元素的宽度和高度的解决方案
1.利用给元素添加行内样式:visibility:hidden;display:block 2.让隐藏元素变成有物理尺寸存在,但不可见,获取元素宽高 3.再给它还原成display为none,去除vi ...
- RabbitMQ消息队列帮助类
调用 //消息队列发消息 MqConfigInfo config = new MqConfigInfo(); config.MQExChange = "DrawingOutput" ...
- Create Table操作
CREATE TABLE 语句 CREATE TABLE 语句用于创建数据库中的表. SQL CREATE TABLE 语法 CREATE TABLE 表名称 ( 列名称1 数据类型, 列名称2 数据 ...
- SMPL模型Shape和Pose参数
两部分 1.Pose参数 2.Shape参数 一 Pose参数 共24个关节点,对应idx从0到23,图中3个小图分别表示zero shape只有idx节点分别绕x/y/z轴旋转. 其中蓝色线表示-p ...
- 6 ~ express ~ 搭建用户注册前端页面
一,前端页面 /views/login.html <!DOCTYPE html> <html lang="en"> <head> <met ...
- errors exist in required project(s) xxx proceed with launch?
原因:本地更新文件和SVN有冲突..找到报错的文件.和SVN资源库的文件对比,修改即可. 一定要解决所有的冲突啊.更新的时候一定要注意有冲突的文件.不然项目无法启动. ---------------- ...
- URAL_1146/uva_108 最大子矩阵 DP 降维
题意很简单,给定一个N*N的大矩阵,求其中数值和最大的子矩阵. 一开始找不到怎么DP,没有最优子结构啊,后来聪哥给了我思路,化成一维,变成最大连续和即可.为了转化成一维,必须枚举子矩阵的宽度,通过预处 ...
- x264报错No working C compiler found.
现象: 缺少C++部署包 解决 [root@localhost x264]# yum -y install gcc gcc-c++ kernel-devel [root@localhost x264] ...
- 开发app
开始学习apicloud开发流程 第一天 了解平台应用 第二天看视频进行学习软件开发的过程 第三天学习编码html 第四天编写了一副框架 第五天完成扫一i扫
- PAT 2012 冬
A Shortest Distance 题意相当于一个环,找出两点间从不同方向得到的距离中的最小值. #include <cstdio> #include <iostream> ...