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. 修改了SpringBoot的主类名称后,gradle build报错的解决办法

    Unable to find a single main class from the following candidates [*.*Application]

  2. python 类的进阶

    类的进阶 一 isinstance(obj,cls)和issubclass(sub,super) class Foo: def __init__(self,name): self.name = nam ...

  3. github生成SSH公钥

    ssh-keygen -t rsa -C "your_email@youremail.com" 然后输入github上的密码 Enter passphrase (empty for ...

  4. echarts版本折线图

    1.效果如下:         绘制折线图,应该算是说echarts中使用最简单也算使用频率最高的一种功能了吧.根据官网列子能找出规律,只是有些属性对于初接触者来说,会有点陌生,不过仔细阅读一下还是不 ...

  5. win10被微软流氓更新后编译基于visual Studio的web项目报[ArgumentOutOfRangeException: 指定的参数已超出有效值的范围

    最近忙得算焦头烂额.就在这个时候.一个不留神.微软的自动更新打开了.这流氓就在我百忙之中强迫我休息了一个多小时. 焦急等待它更新完以后赶紧打开visual studio跑代码.运行好几次都报错.想想不 ...

  6. 老刘 Yii2 源码学习笔记之 Component 类

    类图关系 属性与方法 class Component extends BaseObject { private $_events = []; private $_eventWildcards = [] ...

  7. 手写java虚拟机(一)——搭建环境

    毕业设计打算做一个java虚拟机,首先要对java虚拟机有一个简单的了解(jvm).目前市面上有众多的jvm,如sun公司的HotSpot VM.Classic VM,IBM公司的J9 VM等等,这里 ...

  8. 从零开始系列之vue全家桶(3)安装使用vuex

    什么是vuex? vuex:Vue提供的状态管理工具,用于同一管理我们项目中各种数据的交互和重用,存储我们需要用到数据对象. 即data中属性同时有一个或几个组件同时使用,就是data中共用的属性. ...

  9. Javascript中获取浏览器类型和操作系统版本等客户端信息常用代码

    /** * @author hechen */ var gs = { /**获得屏幕宽度**/ ScreenWidth: function () { return window.screen.widt ...

  10. phantomjs 开发爬虫框架

    函数 page.childframescount page.childframesname page.close page.currentframename page.deletelater page ...