152. Maximum Product Subarray(中等, 神奇的 swap)
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)的更多相关文章
- 152. Maximum Product Subarray - LeetCode
Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
- 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 solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- 【刷题-LeetCode】152 Maximum Product Subarray
Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...
- [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- [LeetCode]152. Maximum Product Subarray
This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...
- LeetCode 152. Maximum Product Subarray (最大乘积子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 152. Maximum Product Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- 【LeetCode】152. Maximum Product Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
随机推荐
- OAuth2.0学习(1-9)新浪开放平台微博认证-web应用授权(授权码方式)
1. 引导需要授权的用户到如下地址: URL 1 https://api.weibo.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&respons ...
- 我的jquery validate 笔记
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF- ...
- margin-top塌陷
margin-top 塌陷 在两个不浮动的盒子嵌套时候,内部的盒子设置的margin-top会加到外边的盒子上,导致内部的盒子margin-top设置失败,解决方法如下: 1.外部盒子设置一个边框: ...
- php 数组对象之间的转换
在之前我写过php返回json数据简单实例 从5.2版本开始,PHP原生提供json_encode()和json_decode()函数,前者用于编码,后者用于解码. 一.json_encode() 1 ...
- 数据库“行专列”操作---使用row_number()over(partition by 分组字段 [order by 排序字段])
测试样例: create table test(rsrp string,rsrq string,tkey string,distan string); '); '); '); '); select * ...
- 使用net.sf.cssbox实现网页截图
需要引用包,在pom.xml中添加引用: <dependency> <groupId>net.sf.cssbox</groupId> <artifactId& ...
- IdentityServer4-介绍大纲(译文)
简介 IdentityServer4是一个基于ASP.NET CORE2使用OAuth2.0协议和OpenID Connect的框架 特性如下: Authentiaction作为一个Service 集 ...
- 使用supervisor管理进程
Supervisor (http://supervisord.org) 是一个用 Python 写的进程管理工具,可以很方便的用来启动.重启.关闭进程(不仅仅是 Python 进程).除了对单个进程的 ...
- springboot启动报错
新建springboot整合aop记录web日志的过程中启动失败 错误如下: ***************************APPLICATION FAILED TO START******* ...
- 【MySQL】通过Binary Log简单实现数据回滚(一)
一.前言 对,没错,我又水了好一阵子,深刻反思寄几.前段时间,工作项目上出于对excel等批量操作可能出现误操作的问题,要求提供一个能够根据操作批次进行数据回滚的能力.在开发的过程中接触到了MySQL ...