【Leetcode】【Medium】Maximum Product Subarray
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的更多相关文章
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- 【刷题-LeetCode】152 Maximum Product Subarray
Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关
Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- 152. Maximum Product Subarray - LeetCode
Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
随机推荐
- 设置MySQL字符集utf8
1.修改mysql 配置文件my.cnf 标签[mysqld]下添加即可 character-set-server = utf8 2.创建数据库时设置字符集 create database db_na ...
- 基础selenium+Python(定位、等待、打印)
1.第一个脚本 # coding = utf-8 from selenium import webdriver browser = webdriver.Firefox() browser.get(&q ...
- ugui的优化
参考文章 https://www.jianshu.com/p/061e67308e5f https://www.jianshu.com/p/8a9ccf34860e http://blog.jobbo ...
- unity状态机实现
刚看了浅墨大神的文章让我对状态机有了进一步的理解 具体实现见装载的状态机文章 首先得有个总状态HeroineBaseState接口,其里面的方法主要是与行为相关的方法,让继承此接口的类来实现的 具体的 ...
- 2.2、js基础---预解析和严格模式
一.语言特性 1.预解析:js会把变量的声明(仅仅是声明)提到顶部,但是不会突破作用域. alert(a);var a= 12; //结果,undefi ...
- div+css 制作表格
<div class="table"> <h2 class="table-caption">花名册:</h2> <di ...
- 【c++】输出文件的每个单词、行
假设文件内容为 1. hello1 hello2 hello3 hello4 2. dsfjdosi 3. skfskj ksdfls 输出每个单词 代码 #include <iostream& ...
- jmeter(1)——环境部署及安装
公司人事还有老大都找我谈了一下2019的目标和技能成长规划,所以整体想了一下,技能方面,自己今年准备从性能测试开始着手,也去咨询了一下大神,切入点最好是工具.性能测试是一门非常庞大的课程,最初级,最入 ...
- SpringBoot和SpringCloud区别
SpringBoot专注于快速方便的开发单个个体微服务. SpringCloud是关注全局的微服务协调整理治理框架,它将SpringBoot开发的一个个单体微服务整合并管理起来, 为各个服 ...
- JQuery对数组的一些操作总结
JQuery对数组的处理非常便捷并且功能强大齐全,一步到位的封装了很多原生js数组不能企及的功能.下面来看看JQuery数组的强大之处在哪. $.each(array, [callback]) 遍历 ...