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.

Idea:

https://leetcode.com/problems/maximum-product-subarray/discuss/

keep max(min) value in imax(imin) varable when pointer = i.

swap起到重要作用.
虽然求子数组最大乘积,但最小乘积也需维护,因为:
A Truth is:
big -> small when (A[i] big when (A[i]

人家想法,自个代码:

\(O(n)\) time, \(O(1)\) extra space.

// https://leetcode.com/problems/maximum-product-subarray/discuss/
// A = [2,3,-2,4] return [2,3] = 6
int maxProduct(vector<int>& A) {
int r = A[0], imax = r, imin = r;
for (int i = 1; i < A.size(); i++) {
// A Truth is:
// big -> small when (A[i] < 0)
// small -> big when (A[i] < 0)
// whatever imax, imin is stored negt or posi in.
if (A[i] < 0)
swap(imax, imin); // keep max(min) value in imax(imin) when pointer = i
imax = max(A[i], imax * A[i]); // 之所以记录 imin,
// 是因为 if(A[i] < 0) swap(imax, imin);
// 用到 imin
imin = min(A[i], imin * A[i]);
r = max(r, imax);
}
return r;
}

152. Maximum Product Subarray(中等, 神奇的 swap)的更多相关文章

  1. 152. Maximum Product Subarray - LeetCode

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

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

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

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

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

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

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

  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

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

  9. 【LeetCode】152. Maximum Product Subarray

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

随机推荐

  1. angular2 学习笔记 ( Dynamic Component 动态组件)

    更新 2018-02-07 详细讲一下 TemplateRef 和 ViewContainerRef 的插入 refer : https://segmentfault.com/a/1190000008 ...

  2. SpringBoot集成Mybatis

    1.创建SpringBoot工程 根据 http://www.cnblogs.com/vitasyuan/p/8765329.html 说明创建SpringBoot项目. 2.添加相关依赖 在pom. ...

  3. jdk的server模式修改无效(关于client和server模式)

    本机为64位操作系统,64位jdk,win10. 修改C:\Program Files\Java\jre8\lib\amd64\jvm.cfg无效. 我的文件的内容为: 原因参考如下: http:// ...

  4. 关于ZK框架的onScroll事件的问题

    由于我现在所在的公司用到的zk框架,遇到了一个需求frozen on top. 简单来说就是滚动超过范围后,希望有一块东西停留在滚动窗口的顶部. 一.zk框架 查看了zk的8.x版本,发现组件的支持的 ...

  5. python之集合,深浅copy

    一. 集合 集合是无序的,不重复的数据集合,它里面的元素是可哈希的(不可变类型),但是集合本身是不可哈希(所以集合做不了字典的键)的.以下是集合最重要的两点: 去重,把一个列表变成集合,就自动去重了. ...

  6. mysql 练习题

    导出现有数据库数据: C:\Users\Administrator>mysqldump -u root db1>D:\agon\db1.sql -p  #结构+数据 mysqldump - ...

  7. c#:实现动态编译,并实现动态MultiProcess功能(来自python multiprocess的想法)

    由于之前一直遇到一些关于并行进行数据处理的时效果往往不好,不管是c#还是java程序都是一样,但是在Python中通过multiprocess实现同样的功能时,却发现确实可以提高程序运行的性能,及服务 ...

  8. python的调试

    调试 程序能一次写完并正常执行的概率很小.总会有各种各样的bug需要修正. 有的bug很简单,看看错误信息就知道,有的bug很复杂,我们需要知道出错时 哪些变量的值是正确的,哪些变量的值是错误的,因此 ...

  9. NSDateFormatter常见的使用方式

    NSDateFormatter是NSFormatter的子类,是用来表示输出的时间格式. 下面贴出两个时间的例子. 例子一. - (void)dateString1 { NSDateFormatter ...

  10. Redis Cluster 4.0 on CentOS 6.9 搭建

    集群简介 Redis 集群是一个可以在多个 Redis 节点之间进行数据共享的设施(installation). Redis 集群不支持那些需要同时处理多个键的 Redis 命令, 因为执行这些命令需 ...