热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何切出最大长度乘积MaximumProductCutting@geeksforgeeks

转自出处Givenaropeoflengthnmeters,cuttheropeindifferentpartsofintegerlengthsinawaythatmaximize

转自出处


Given a rope of length n meters, cut the rope in different parts of integer lengths in a way that maximizes product of lengths of all parts. You must make at least one cut. Assume that the length of rope is more than 2 meters.

Examples:

Input: n = 2
Output: 1 (Maximum obtainable product is 1*1)Input: n = 3
Output: 2 (Maximum obtainable product is 1*2)Input: n = 4
Output: 4 (Maximum obtainable product is 2*2)Input: n = 5
Output: 6 (Maximum obtainable product is 2*3)Input: n = 10
Output: 36 (Maximum obtainable product is 3*3*4)

1) Optimal Substructure: 
This problem is similar to Rod Cutting Problem. We can get the maximum product by making a cut at different positions and comparing the values obtained after a cut. We can recursively call the same function for a piece obtained after a cut.

Let maxProd(n) be the maximum product for a rope of length n. maxProd(n) can be written as following.

maxProd(n) = max(i*(n-i), maxProdRec(n-i)*i) for all i in {1, 2, 3 .. n}

2) Overlapping Subproblems
Following is simple recursive C++ implementation of the problem. The implementation simply follows the recursive structure mentioned above.


A Tricky Solution:
If we see some examples of this problems, we can easily observe following pattern.
The maximum product can be obtained be repeatedly cutting parts of size 3 while size is greater than 4, keeping the last part as size of 2 or 3 or 4. For example, n = 10, the maximum product is obtained by 3, 3, 4. For n = 11, the maximum product is obtained by 3, 3, 3, 2. Following is C++ implementation of this approach.



[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package DP;  
  2.   
  3. public class MaxProductCutting {  
  4.   
  5.     public static void main(String[] args) {  
  6.         System.out.println(maxProdRec(10));  
  7.         System.out.println(maxProdDP(10));  
  8.         System.out.println(maxProdTrick(10));  
  9.     }  
  10.       
  11.     public static int maxProdRec(int n){  
  12.         if(n==0 || n==1){  
  13.             return 0;  
  14.         }  
  15.         int max = 0;  
  16.         for(int i=1; i
  17.             // 1.只切一刀  2.切完一刀后,把余下的继续切  
  18.             int bigger = Math.max(i*(n-i), i*maxProdRec(n-i));  
  19.             max = Math.max(max, bigger);  
  20.         }  
  21.           
  22.         return max;  
  23.     }  
  24.       
  25.     // Time: O(n^2), space:O(n)  
  26.     public static int maxProdDP(int n){  
  27.         // maxProd[i]: 总长度为i的绳子能切出的最大乘积  
  28.         int[] maxProd = new int[n+1];  
  29.         maxProd[0] = maxProd[1] = 0;  
  30.           
  31.         // Build the table maxProd[] in bottom up manner and return  
  32.         // the last entry from the table  
  33.         for(int i&#61;1; i<&#61;n; i&#43;&#43;){     // 总长度为i  
  34.             int max &#61; 0;  
  35.             for(int j&#61;1; j<&#61;i/2; j&#43;&#43;){   // 切长度为j  
  36.                 int bigger &#61; Math.max(j*(i-j), j*maxProd[i-j]);  
  37.                 max &#61; Math.max(max, bigger);  
  38.             }  
  39.             maxProd[i] &#61; max;  
  40.         }  
  41.         return maxProd[n];  
  42.     }  
  43.       
  44.     // 规律&#xff1a;不断以3为单位长度切  
  45.     public static int maxProdTrick(int n){  
  46.         if(n&#61;&#61;2 || n&#61;&#61;3){       // n equals to 2 or 3 must be handled explicitly  
  47.             return n-1;  
  48.         }  
  49.         int res &#61; 1;  
  50.         while(n > 4){        // Keep removing parts of size 3 while n is greater than 4  
  51.             n -&#61; 3;  
  52.             res *&#61; 3;       // Keep multiplying 3 to res  
  53.         }  
  54.         return n*res;       // The last part multiplied by previous parts  
  55.     }  
  56.   
  57. }  





推荐阅读
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • C# 7.0 新特性:基于Tuple的“多”返回值方法
    本文介绍了C# 7.0中基于Tuple的“多”返回值方法的使用。通过对C# 6.0及更早版本的做法进行回顾,提出了问题:如何使一个方法可返回多个返回值。然后详细介绍了C# 7.0中使用Tuple的写法,并给出了示例代码。最后,总结了该新特性的优点。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • 从零学Java(10)之方法详解,喷打野你真的没我6!
    本文介绍了从零学Java系列中的第10篇文章,详解了Java中的方法。同时讨论了打野过程中喷打野的影响,以及金色打野刀对经济的增加和线上队友经济的影响。指出喷打野会导致线上经济的消减和影响队伍的团结。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文介绍了在Java中gt、gtgt、gtgtgt和lt之间的区别。通过解释符号的含义和使用例子,帮助读者理解这些符号在二进制表示和移位操作中的作用。同时,文章还提到了负数的补码表示和移位操作的限制。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
author-avatar
狮子座YAO
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有