[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%的值落于正负标准差之 ...
随机推荐
- Django ORM中常用的字段类型以及参数配置
一.数值型 AutoField对应int(11).自增主键,Django Model默认提供,可以被重写. BooleanField对应tinyint(1).布尔类型字段,一般用于记录状态标记. De ...
- 【踩坑记录】记录一次使用Python logging库多进程打印日志的填坑过程
背景: 项目使用Python自带的logging库来打印日志 项目部署在一台Centos7的机器上 项目采用gunicorn多进程部署 过程: 1.LOG日志代码封装: 采用logging库,并设置w ...
- JAVA中添加jar包
右键点击工程文件,选择构建路径>添加外部归档.选择包的路径即可
- opencv目录(转)
github:https://github.com/opencv/opencv OpenCV 3 的源代码文件夹: 3rdparty/: 包含第三方库,如用视频解码用的 ffmpeg.jpg.png. ...
- python 导入数据包的几种方法
1.直接导入整个数据包:improt 数据包 参考代码: # -*- coding:utf-8 -*- # 导入random数据包 import random # 引用random数据包中的randi ...
- Docker每次启动容器,IP及hosts指定
原文链接:https://blog.csdn.net/u012834750/article/details/80508464 前言 每次在使用Docker启动Hadoop集群的时候,都需要重新绑定下网 ...
- Neo4j--UNIQUE约束
UNIQUE简介 和关系型数据库一样,对数据进行约束作用. 比如在某个属性上不能插入重复的节点. 比如属性的完整性约束. 创建UNIQUE约束 创建UNIQUE语法 CREATE CONSTRAINT ...
- Python 官方推荐的一款打包工具
译者:Jiong 链接: https://robots.thoughtbot.com/how-to-manage-your-python-projects-with-pipenv 在thoughtbo ...
- 实验吧web-中-忘记密码了
打开网页,查看源代码,好像发现了管理员邮箱而且还是用vim编辑的. 我们提交一下这个邮箱,虽然提交成功了,但好像并没什么用. 我们随便提交一个,会弹出 看来好像还有个step2呢,我们查看源代码(在这 ...
- python np array转json
np array转json import numpy as np import codecs, json a = np.arange().reshape(,) # a by array b = a.t ...