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,不同的是

  • max(max_local*nums[i], nums[i])表示:如果之前的数max_local和nums[i]符号相反,那么nums[i]会是max
  • max(max(max_local*nums[i], nums[i]), min_local*nums[i])表示:如果nums[i]是个负数,那么就可能和min_local组成局部最优=>这是为什么我们要设置min_local
class Solution {
public:
int maxProduct(vector<int>& nums) {
int max_local = nums[];
int min_local = nums[];
int max_copy;
int global = nums[]; for(int i = ; i < nums.size(); i++){
max_copy = max_local;
max_local = max(max(max_local*nums[i], nums[i]), min_local*nums[i]);
min_local = min(min(max_copy*nums[i], nums[i]), min_local*nums[i]);
global = max(global,max_local);
} return global;
}
};

152. Maximum Product Subarray (Array; DP)的更多相关文章

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

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

  2. 152. Maximum Product Subarray - LeetCode

    Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...

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

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

  4. 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray

    题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...

  5. [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  6. [LeetCode]152. Maximum Product Subarray

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

  7. LeetCode 152. Maximum Product Subarray (最大乘积子数组)

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

  8. 152. Maximum Product Subarray(动态规划)

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  9. 152. Maximum Product Subarray最大乘积子数组/是否连续

    [抄题]: Given an integer array nums, find the contiguous subarray within an array (containing at least ...

随机推荐

  1. Node 在 Centos7 系统下的安装

    1,下载二进制包  https://nodejs.org/zh-cn/download/     根据自己的需求选择对应的版本,不推荐使用源码包,容易出现错误 2,上传到 linux 服务器 3, 解 ...

  2. day05-表的三种关系

    表的三种关系 1)一对一 关联方式:foreign key+unique例如1个学生只能有1个学号,1个学号只能对应1个学生 #两张表: 学生表(student)和 学号表(student_id) # ...

  3. 自动调整linux系统时间和时区与Internet时间同步

    调整linux系统时间和时区与Internet时间同步 一.修改时区:# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime修改为中国的东八区# v ...

  4. js删除dom节点时候索引出错问题

    我们知道删除一个dom节点的时候索引就会发生了改变,甚至是错误,就算jq的ecah也无能为力,所以我们只能自己写个功能了 直接上代码把,不多说 <!DOCTYPE html> <ht ...

  5. JSdom操作内容,样式,属性

    <p> JavaScript 能够直接写入 HTML 输出流中: </p> <script> document.write("<h1>This ...

  6. vue .map 文件

    参数: productionSourceMap:false 这个改为false.去掉打包产生的map文件 map文件的作用:定位线上错误代码位置;

  7. linux查询硬件信息

    硬件信息查询 sudo dmidecode -t baseboard

  8. Python : 什么是*args和**kwargs

    让生活Web个够 先来看个例子: def foo(*args, **kwargs): print 'args = ', args print 'kwargs = ', kwargs print '-- ...

  9. 计算机&通信词典

    目录 A B C Cgroups D E F G H I J K L M N NFV NFV ISG O ONF P Q R Rewrite S T U V VNFI W X Y Z A B C Cg ...

  10. 静态函数造成GC的原因

    有时候用deep profiling查看GC时会发现:一个父函数有GC,展开子层级看到一个很奇怪的 CX::ctor,表示CX进行了构造,然后打开父函数代码却完全看不到有new CX的地方,这个时候可 ...