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. 十三、SAP中定义变量时赋初始值

    一.代码如下 二.输出如下

  2. React 学习笔记(2) 路由和UI组件使用

    安装依赖 cnpm install react-router-dom -S // 或 yarn add react-router-dom 导入 // index.js import React fro ...

  3. GAN评价指标之mode score

    通过 Inception Score 的公式我们知道,它并没有利用到真实数据集的信息,所有的计算都在生成的图片上计算获得.而 Mode Score 基于此做了改进: 也就是说,想要提高 Mode Sc ...

  4. Mysql :分支结构—if函数

    一分支结构 1.if函数 功能:实现简单的双分支 语法: IF (表达式1,表达式2,表达式3) 执行顺序 如果表达式1成立 则if函数返回表达式2的值,否则返回表达式3的值 应用: 任何地方

  5. 小程序实现textarea行数自动增加

    查找网上案例很多,但是都不是很满意,参考大牛案例终结了一下,话不多说代码如下: 实现效果: 前段代码 <view class="text-box"> <view& ...

  6. (转) Windows如何区分鼠标双击和两次单击

    Windows如何区分鼠标双击和两次单击 http://lbsloveldm.blog.hexun.com/12212875_d.html 在Windows平台上,鼠标左键的按下.松开.快速的两次点击 ...

  7. k8s deployment yam 文件分析

    apiVersion: extensions/v1beta1 kind: Deployment metadata: name: namespace: labels:spec: replicas: #设 ...

  8. 005、mysql查询表的结构

    EXPLAIN hcc_ip 如果有一个表,表明为hcc_ip,使用以上语句可以得到下图 不忘初心,如果您认为这篇文章有价值,认同作者的付出,可以微信二维码打赏任意金额给作者(微信号:38247724 ...

  9. 【机器学习实战学习笔记(1-1)】k-近邻算法原理及python实现

    笔者本人是个初入机器学习的小白,主要是想把学习过程中的大概知识和自己的一些经验写下来跟大家分享,也可以加强自己的记忆,有不足的地方还望小伙伴们批评指正,点赞评论走起来~ 文章目录 1.k-近邻算法概述 ...

  10. 如何做好Essay Proofreading?

    终于写完了一篇Essay,瞬间感觉人生轻松了好多!别急,想要Essay最终得到高分,你还需要最后一步:论文润色! 换句话说,就是我们需要写完的Essay进行检查校对.那么我们一般需要校对的部分有哪些呢 ...