LeetCode 笔记26 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.
mlgb的,今天遇到这个题搞了半天。记录下。
首先解放可以使用穷举。通过二维数组product[i][j]记录从i到j的乘积,然后遍历得到数组中最大元素,就是答案。
public int maxProduct(int[] A) {
int[][] product = new int[A.length][A.length];
int max = A[0];
for(int i = 0; i < A.length; i++) {
product[i][i] = A[i];
for (int j = i + 1; j < A.length; j++) {
product[i][j] = product[i][j - 1] * A[j];
max = Math.max(max, product[i][j]);
}
max = Math.max(max, A[i]);
}
return max;
}
简单明了。但是当然是超时。
然后撸主想了很久,期间还看了部电影。
假设第i元素是个正数,那么我们要计算当前i个元素的最大乘积p[i],就要 知道前面i-1的最大乘积p[i-1],p[i] = p[i - 1] * A[i];
假设第i元素是个负数,那么我们要计算当前i个元素的最大乘积p[i],就要知道前面i-1的最小乘积(负数)n[i-1], p[i] = n[i - 1] * A[i];
p[i]表示以第i元素结尾,能取到的最大乘积;n[i]表示以第i元素结尾,能取到的负乘积。如果i元素是0的花,一切归0,p[i] = n[i] = 0.
public int maxProduct2(int[] A) {
if (A.length == 1) {
return A[0];
}
int[] p = new int[A.length];
int[] n = new int[A.length];
int max = Integer.MIN_VALUE;
for (int i = 0; i < A.length; i++) {
if (A[i] > 0) {
p[i] = (i > 0 && p[i - 1] > 0) ? p[i - 1] * A[i] : A[i];
n[i] = (i > 0 && n[i - 1] < 0) ? n[i - 1] * A[i] : 0;
} else if (A[i] < 0) {
p[i] = (i > 0 && n[i - 1] < 0) ? n[i - 1] * A[i] : 0;
n[i] = (i > 0 && p[i - 1] > 0) ? p[i - 1] * A[i] : A[i];
} else {
max = Math.max(max, 0);
}
if (p[i] > 0) {
max = Math.max(max, p[i]);
} else if (n[i] < 0) {
max = Math.max(max, n[i]);
}
}
return max;
}
因为遇到0的时候,其实是新的起点。
上面的代码可以简化为使用常量空间的下面的可读性较差的代码。
public int maxProduct(int[] A) {
if (A.length == 1) {
return A[0];
}
int p = 0;
int n = 0;
int max = Integer.MIN_VALUE;
for (int i = 0; i < A.length; i++) {
int np = p > 0 ? p * A[i] : A[i];
int nn = n < 0 ? n * A[i] : 0;
if (A[i] > 0) {
p = np;
n = nn;
} else if (A[i] < 0) {
p = nn;
n = np;
} else {
p = 0;
n = 0;
max = Math.max(max, 0);
continue;
}
if (p > 0) {
max = Math.max(p, max);
} else if (n < 0) {
max = Math.max(n, max);
}
}
return max;
}
LeetCode 笔记26 Maximum Product Subarray的更多相关文章
- 【刷题-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
This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...
- 【LeetCode】152. Maximum Product Subarray 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 动态规划 参考资料 日期 题目地址:htt ...
- LeetCode OJ 152. Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【LeetCode】152. Maximum Product Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- LeetCode OJ:Maximum Product Subarray(子数组最大乘积)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 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 ...
随机推荐
- 导出excel乱码问题
今天遇到一个问题,在用C#做导出excel的时候,出现excel乱码问题.百度了下. 发现问题如下: 非中文字符编码问题. 解决方法: 把输出的excel格式设置成UTF-8即可. 更改代码: Res ...
- JQuery怎么实现页面刷新后保留鼠标点击样式的方法
今天抽空研究了下鼠标点击添加样式的方法.为了防止忘记,写篇文章算是备份吧. $('ul.main-menu li a').each(function(){ if($($(this))[0].h ...
- 大家一起和snailren学java-(四)初始化与清理
初始化和清理,是一个生命周期的起始.在java中,对象的初始化和对象的回收是怎样的呢? 在开发c++的时候,有构造器的概念.即对象的创建,首先默认调用构造器进行初始化.在java中也有“构造器”.ja ...
- INFORMATICA 的调优之 INFORMATICA SERVER TUNING
INFORMATICA SERVER的调优我认为主要从两个级别来做,一个是MAPPING级别,一个是SESSION级别. 对于MAPPING级别的调优: 一 对MAPPING数据流向的优化: 1 控 ...
- PowerDesigner执行SQL生成模型
PowerDesigner版本:15.2.0 步骤如下: 1.打开PowerDesigner软件如下图: 2.选择:File->Reverse Engineer->Database... ...
- Apache CXF自定义拦截器
为什么设计拦截器?1.为了在webservice请求过程中,能动态操作请求和响应数据,CXF设计了拦截器 拦截器分类: 1.按所处的位置分:服务器端拦截器,客户端拦截器. 2.按消息的方向分:入拦截器 ...
- 利用jsp和servlet,MySQL实现简易报表
beans包和jdbc包代码不放了,麻烦 Service.java: package service; import java.sql.Connection;import java.sql.Resul ...
- sublime生产力提升利器
sublime 操作快捷键功能-生产力提升利器 Go to anything ctrl+p 支持快速模糊匹配 查找替换 ctrl+h 多行游标(当只需查找/替换/选中部分相同内容时)有以下方式来产 ...
- FileInputFormat
MapReduce框架要处理数据的文件类型 FileInputFormat这个类决定. TextInputFormat是框架默认的文件类型,可以处理Text文件类型,如果你要处理的文件类型不是Text ...
- Java : 使用jar包里的图片作为窗体的ICON
文件结构: 源包- -/code/Jframe1.java -/image/1.png 目标: Jframe1.java 使用"/image/1.png"作为左上角的icon 核心 ...