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 ...
随机推荐
- DP大作战——多重背包
题目描述 在之前的上机中,零崎已经出过了01背包和完全背包,也介绍了使用-1初始化容量限定背包必须装满这种小技巧,接下来的背包问题相对有些难度,可以说是01背包和完全背包的进阶问题. 多重背包:物品可 ...
- IO流02--毕向东JAVA基础教程视频学习笔记
提要 08 自定义装饰类09 LineNumberReader10 MyLineNumberReader11 字节流File读写操作12 拷贝图片13 字节流的缓冲区14 自定义字节流的缓冲区-rea ...
- 多线程--毕向东java基础教程视频学习笔记
目录 1.多线程运行的安全问题 2.多线程同步代码块 3.同步方法的锁是this 4.静态同步方法的锁是Class对象 5.单例设计模式(面试中的考点) 6.死锁(一个发生死锁的例子) 多线程运行的安 ...
- MySQL数据库初识(一)
MySQL是一种免费的小型关系型数据库,与Linux.Apache/Nginx.PHP一起组成了WEB开发的黄金搭档. MySQL是C/S(客户端/服务端)体系结构的软件,而在开发中,PHP承担起了客 ...
- Web Service和WCF的到底有什么区别
[1]Web Service:严格来说是行业标准,也就是Web Service 规范,也称作WS-*规范,既不是框架,也不是技术. 它有一套完成的规范体系标准,而且在持续不断的更新完善中. 它使用XM ...
- java微信接口之五—消息分组群发
一.微信消息分组群发接口简介 1.请求:该请求是使用post提交地址为: https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_t ...
- Ubuntu Server安装图形界面全过程
转载自:http://blog.csdn.net/sunbaigui/article/details/6624110, http://mikewolfli.blog.163.com/blog/stat ...
- Ubuntu16.04安装ROS-kinetic
参考链接:http://www.voidcn.com/blog/wishchin/article/p-5972036.html 第一步: 软件源配置1. 增加下载源(增加ubuntu版的ros数据仓库 ...
- A python tool to static sim.log duration time
When working ALU IMS Patch team, we need to static the SU duration to add it to the patch report, th ...
- SSIS WITH VERTICA的注意事项总结
项目中使用到SSIS访问vertica,以下是我使用中总结的一些注意事项: 1.vertica对约束的处理方式与之前的DBMS是不同的. 以上的截图说明,在使用ado.net provider连接ve ...