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. 新概念英语(1-21)Whick book

    Which book does the man want? A:Give me a book, please, Jane? B:Whick book? this one ? A:No, not tha ...

  2. Spark入门(1-1)什么是spark,spark和hadoop

    一.Spark是什么? Apache Spark 是专为大规模数据处理而设计的快速通用的计算引擎,可用来构建大型的.低延迟的数据分析应用程序. Spark是UC Berkeley AMP lab (加 ...

  3. 2018年html5入门到精通教程电子书百度云盘下载共22本

    名称 查看 <HTML5启动和运行>(HTML5.Up.and.Running)扫描版[PDF] 下载 <Pro HTML5 Performance>(Pro HTML5 Pe ...

  4. Hive:有表A与表B进行inner join,如果A分组内包含有数据,使用A,否则使用B分组下的数据

    tommyduan_fingerlib 指纹库 栅格小区级别数据tommyduan_mr_grid_cell_result_all 统计 栅格小区级别数据业务:以tommyduan_mr_grid_c ...

  5. html标记语言 --文本标记

    html标记语言 --文本标记 二.文本标记 1.h1-h6 标题标记,h1最大 2.font 字体设置标记 2.1 size字体大小.<font size="> 取值范围1-7 ...

  6. LabelFrame

    LabelFrame组件是Frame组件的变体. 默认情况下,LabelFrame会在其子组件的周围绘制一个边框以及一个标题. 何时使用LabelFrame组件?当你想要奖一些相关的组件分为一组的时候 ...

  7. CSS 选择器简介

    前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! 选择器权重 如果以4位数表示选择符权重,那么: 元素选择器的权重是1: id 选择器的权重为100: cl ...

  8. Eclipse代码块折叠插件,安装使用

    在代码编写中经常会遇到一些很长的set(xxx)的代码,非常影响体验. 而Eclipse的folding插件可以自定义的将代码块进行折叠. 效果如下图所示: 可以根据代码块的功能来进行折叠,从而保证代 ...

  9. [LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...

  10. 跨域访问 - 跨域请求 同源策略概念对跨域请求的影响 及几种解决跨域请求的方法如 jsonp

    为什么会设置同源策略 > 适用于浏览器的一种资源访问策略 > 同源策略(Same origin policy)是一种约定,它是浏览器最核 心也最 基本的安全功能,如果缺少了同源策略,则浏览 ...