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.

解题思路:

求连续子串和/积系列,最优的方法是只遍历一遍,实现o(n)的时间复杂度。

思考:

1、对于子序列中连续的正数,那么乘积也是不断的累积,如果遇到了负数,则最大乘积值暂时为前面正数的累积;

如果按照这个思路,只需要记录连续正数乘积最高,即为最大子序列积;那么这样就实现了o(n)的复杂度;

2、但是上面思路忽略了子序列中包含两个负数,负负得正,依然可以成就最大子序列积;

针对这种情况,在遍历的时候,不仅记录乘积最大值,还记录乘积最小值,乘积最小值

【Leetcode】【Medium】Maximum Product Subarray的更多相关文章

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

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

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

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

  3. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  4. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  5. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  6. LeetCode Maximum Product Subarray(枚举)

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

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

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

  8. LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关

    Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...

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

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

  10. 152. Maximum Product Subarray - LeetCode

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

随机推荐

  1. 仔细说明CLOSE_WAIT状态的原因

    CLOSE_WAIT状态!!! 服务器收到客户端FIN报文后  服务器端口回复了ack,没有回复FIN报文. 1 说明客户端后面不会再向服务器发送用户层的数据了, 2 客户端 没有回复FIN 报文嘛 ...

  2. Centos6.5上安装sonarqube6.7.6

    Centos6.5已经装备好,可以联网 sonarqube6.7.6下载地址:https://www.sonarqube.org/downloads/ 步骤: 安装MySQL5.7 MySQL详细的安 ...

  3. Git学习系列之集中式版本控制系统vs分布式版本控制系统

    不多说,直接上干货! Linus一直痛恨的CVS及SVN都是集中式的版本控制系统,而Git是分布式版本控制系统,集中式和分布式版本控制系统有什么区别呢? 先说集中式版本控制系统,版本库是集中存放在中央 ...

  4. Git学习系列之Git是什么?

    前言 现在主流IDE里,都集成git了. https://git-scm.com/docs 史上最浅显易懂的Git教程! 为什么要编写这个教程?因为我在学习Git的过程中,买过书,也在网上Google ...

  5. vue-cli 基础搭建

    1.安装node 2.npm install webpack -g 3.npm install vue-cli -g 4.然后进入到文件下边 vue init webpack 文件名字 5.进入工程文 ...

  6. java 配置在.properties文件中的常量

    不让用常量类,那就用.properties文件配置,放在根目录. import java.util.HashMap; import java.util.Iterator; import java.ut ...

  7. c# winfrom 页面的enter变为tab 功能使用 在特定的按钮里面如何继续当enter使用求大神帮忙解答一下 !!急

    enter 当tab  键用 已经实现  :例如按回车的时候切换一直走 ,走到一个按钮如何让回车键在这个按钮的时候还是执行enter按钮的功能而不是tab   求大神解答一下, 目前页面tab功能改为 ...

  8. C#继承中构造函数,成员变量的执行顺序

    public class Animal { static string baseClassName; protected string _skincolor; Instancevariable iv ...

  9. C#根据用户输入字符串,输出大写字母有几个,小写字母有几个

    static void Main(string[] args) { // 根据用户输入字符串,输出大写字母有几个,小写字母有几个. Console.WriteLine("请输入一行英文代码& ...

  10. 关于request请求的基本获取

      1.Request对象的作用是与客户端交互,收集客户端的Form.Cookies.超链接,或者收集服务器端的环境变量. request对象是从客户端向服务器发出请求,包括用户提交的信息以及客户端的 ...