题目链接

  题目要求:

  Find the contiguous subarray within an array (containing at least one number) which has the largest product.

  For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6.

  题目分析参考自一博文

  这道题跟Maximum Subarray模型上和思路上都比较类似,还是用一维动态规划中的“局部最优和全局最优法”。这里的区别是维护一个局部最优不足以求得后面的全局最优,这是由于乘法的性质不像加法那样,累加结果只要是正的一定是递增,乘法中有可能现在看起来小的一个负数,后面跟另一个负数相乘就会得到最大的乘积。不过事实上也没有麻烦很多,我们只需要在维护一个局部最大的同时,再维护一个局部最小,这样如果下一个元素遇到负数时,就有可能与这个最小相乘得到当前最大的乘积和,这也是利用乘法的性质得到的。

 class Solution {
public:
int maxThree(int a, int b, int c)
{
int tmp = (a > b) ? a : b;
return (tmp > c) ? tmp : c;
} int maxProduct(vector<int>& nums) {
int sz = nums.size();
if(sz == )
return ; int max_local = nums[];
int min_local = nums[];
int global = nums[];
for(int i = ; i < sz; i++)
{
int max_copy = max_local;
max_local = max(max(max_local * nums[i], nums[i]), min_local * nums[i]);
min_local = min(min(min_local * nums[i], nums[i]), max_copy * nums[i]);
global = max(global, max_local);
} return global;
}
};

LeetCode之“动态规划”:Maximum Product Subarray的更多相关文章

  1. 【刷题-LeetCode】152 Maximum Product Subarray

    Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...

  2. [LeetCode]152. Maximum Product Subarray

    This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...

  3. 【LeetCode】152. Maximum Product Subarray 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 动态规划 参考资料 日期 题目地址:htt ...

  4. 【LeetCode】152. Maximum Product Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  5. LeetCode OJ:Maximum Product Subarray(子数组最大乘积)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  6. LeetCode 笔记26 Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  7. LeetCode OJ 152. Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  8. LeetCode Maximum Product Subarray(枚举)

    LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...

  9. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  10. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

随机推荐

  1. Matlab:如何查找给定目录下的文件

    我们有很多目录,每个目录下都有些有用的文件,比如图像文件,如何自动的扫描这些文件呢? 可以使用dir函数来完成这个任务. 比如假设给定目录 baseDir,它是一个字符串,包含的是某个目录,例如'./ ...

  2. Python Skelve 库

    在Python中有一个简单的轻量级的类似于Key-value的存储型数据库,那就是Skelve.下面就来一起看一看这个库的简单的使用吧. 小例子 我本人比较喜欢从例子出发,然后再来研究这些内部的行为. ...

  3. Android官方推荐使用DialogFragment替换AlertDialog

    DialogFragment是在Android3.0(API level 11)中引入的,它代替了已经不建议使用的AlertDialog. DialogFragment高效地封装和管理对话框的生命周期 ...

  4. 08 ListView 优化

    ListVie的优化 1 固定ListView长宽高 如下图在清单文件中: <ListView android:id="@+id/lv" android:layout_wid ...

  5. 01_Linux系统系统语言查询,设置Xshell工具,中文显示,测试Xshell中文字符显示,Linux中文显示乱码设置

              Xshell是一个强大的安全终端模拟软件,它支持SSH1,SSH2,以及Microsoft Windows平台的TELNETNetSarang Xshell 4 Build 0 ...

  6. install_driver(Oracle) failed: Can't load `.../DBD/Oracle/Oracle.so' for module DBD::Oracle

    Description This section is from the "Practical mod_perl " book, by Stas Bekman and Eric C ...

  7. Cocos2D将v1.0的tileMap游戏转换到v3.4中一例(七)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 打开SpriteBuilder,在文件视图中新建一个文件夹Fon ...

  8. 使用UE4/Unity创建VR项目

    一.主要的步骤是说一下使用UE4,在此之前先说一下使用unity创建的VR项目 1.unity创建oculus rift dk2项目 在unity中创建一个简单的场景,让摄像机能看见场景中的物体,不对 ...

  9. ROS_Kinetic_18 使用V-Rep3.3.1和Matlab2015b(vrep_ros_bridge)续

    ROS_Kinetic_18 使用V-Rep3.3.1和Matlab2015b(vrep_ros_bridge)续 上一节配置的v-rep在ros kinetic中是可以看图像,并订阅主题的,但是无法 ...

  10. 1032. Sharing (25) -set运用

    题目如下: To store English words, one method is to use linked lists and store a word letter by letter. T ...