Leetcode152. Maximum Product Subarray乘积的最大子序列
给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数)。
示例 1:
输入: [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] 有最大乘积 6。
示例 2:
输入: [-2,0,-1] 输出: 0 解释: 结果不能为 2, 因为 [-2,-1] 不是子数组。
维护三个变量:局部最大值,局部最小值,总最大值
class Solution {
public:
int maxProduct(vector<int>& nums)
{
int len = nums.size();
if(len <= 1)
{
return len == 0? 0 : nums[0];
}
int min_local = nums[0];
int max_local = nums[0];
int res = nums[0];
for(int i = 1; i < len; i++)
{
int temp = max_local;
max_local = max(max(max_local * nums[i], nums[i]), nums[i] * min_local);
min_local = min(min(min_local * nums[i], nums[i]), temp * nums[i]);
res = max(max_local, res);
}
return res;
}
};
Leetcode152. Maximum Product Subarray乘积的最大子序列的更多相关文章
- 152 Maximum Product Subarray 乘积最大子序列
找出一个序列中乘积最大的连续子序列(该序列至少包含一个数).例如, 给定序列 [2,3,-2,4],其中乘积最大的子序列为 [2,3] 其乘积为 6.详见:https://leetcode.com/p ...
- 求连续最大子序列积 - 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】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- LeetCode_Maximum Subarray | Maximum Product Subarray
Maximum Subarray 一.题目描写叙述 就是求一个数组的最大子序列 二.思路及代码 首先我们想到暴力破解 public class Solution { public int maxSub ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- 【刷题-LeetCode】152 Maximum Product Subarray
Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...
- 152. Maximum Product Subarray - LeetCode
Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
随机推荐
- 9.spark Core 进阶2--Cashe
RDD Persistence One of the most important capabilities in Spark is persisting (or caching) a d ...
- AIO 详解
AIO(Asynchronous Input and Output) 异步IO则采用"订阅-通知"模式: 即应用程序向操作系统注册IO监听,然后继续做自己的事情. 当操作系统发生I ...
- Delphi利用Windows GDI实现文字倾斜
Delphi利用Windows GDI实现文字倾斜 摘要 Delphi利用Windows GDI实现文字倾斜 procedure TForm1.FormPaint(Sender: TObject);v ...
- JAVA 文件的上传下载
一.上传文件 1.使用 transferTo 上传 @ResponseBody @RequestMapping(value = "/file/upload") public Res ...
- Delphi代码创建形式规范 1.0
Delphi代码创建形式规范 1.0 本规范的目的:给自己的代码一个统一而标准的外观,增强 可读性,可理解性,可维护性 本规范的原则:名称反映含义,形式反映结构 1.单元风格 ...
- PAT甲级——A1135 Is It A Red-Black Tree 【30】
There is a kind of balanced binary search tree named red-black tree in the data structure. It has th ...
- 第二十篇:记下第一个mysql触发器
项目背景:给一个服务限制访问次数,当用户访问这个服务的次数达到这个值的时候,关闭他的访问权限首先访问信息存在一张表中,记录用户的ip:visitor_ip,服务的id:service_id,访问次数: ...
- vue-router路由跳转判断用户是否存在
router.beforeEach((to, from, next) => { //console.log("to:", (to)); //console.log(" ...
- WPF基础之Grid面板
一.显示 Grid的线条,设置ShowGridLiens="True".
- linux 文件类型的颜色
linux文件颜色的含义:蓝色代表目录 绿色代表可执行文件 红色表示压缩文件 浅蓝色表示链接文件 灰色表示其他文件 红色闪烁表示链接的文件有问题了 黄色表示设备文件 蓝色文件----------目录 ...