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. python爬虫专栏学习

    知乎的一个讲python的专栏,其中爬虫的几篇文章,偏入门解释,快速看了一遍. 入门 爬虫基本原理:用最简单的代码抓取最基础的网页,展现爬虫的最基本思想,让读者知道爬虫其实是一件非常简单的事情. 爬虫 ...

  2. Vector bit-select and part-select addressing verilog片选写法

    大端 m m[ a +: b ] == m[ (a+b-1) : a ] m[ a -: b ] == m[ a : (a-b+1) ] 小端 n n[ a +: b ] == n[ a : (a+b ...

  3. 如何在react&webpack中引入图片?

    在react&webpack项目中需要引入图片,但是webpack使用的模块化的思想,如果不进行任何配置,而直接在jsx或者是css中使用相对路径来使用就会出现问题,在webpack中提供了u ...

  4. python中range()和len()函数区别

    函数:len() 作用:返回字符串.列表.字典.元组等长度 语法:len(str) 参数: str:要计算的字符串.列表.字典.元组等 返回值:字符串.列表.字典.元组等元素的长度 实例 1.计算字符 ...

  5. 没有循环的JavaScript

    有些文章中提到过,缩进(并不能特别准确的)说明了代码的复杂程度.我们想要的是简单的JavaScript.之所以层层缩进,是因为我们用抽象的方式解决问题.但要选用什么抽象方法呢?截止目前,我们没有在特定 ...

  6. idea中运行maven安装jar包到本地仓库跳过test

  7. 垂直居中问题不只有 line-height 可以解决,还有一个哥们叫 margin-top

    我们都知道,对于一行文本的垂直居中可以通过设置 height 与 line-height 值相等来实现. 那么对于两个嵌套的div ,或者一个div中的多行文本,怎么让被包含的部分实现垂直居中呢?显然 ...

  8. 计算F1Score

    计算F1Score predictions = pval < epsilon fp = sum((predictions == 1) & (y == 0)) fn = sum((pred ...

  9. datatable填装List代替for循环

    public class DataToModelHelper<T> where T : new() { public static IList<T> ConvertToMode ...

  10. dbcp数据库连接池的java实现

    1.准备 导入jar包 commons-dbcp-1.4.jar commons-pool-1.3.jar 数据库驱动包,如:mysql-connector-java-5.1.28-bin.jar 2 ...