LeetCode Maximum Product Subarray 解题报告
LeetCode 新题又更新了。求:最大子数组乘积。
https://oj.leetcode.com/problems/maximum-product-subarray/
题目分析:求一个数组,连续子数组的最大乘积。
解题思路:最简单的思路就是3重循环。求解productArray[i][j]的值(productArray[i][j]为A[i]到A[j]的乘积),然后记录当中最大值,算法时间复杂度O(n3)。必定TLE。
第一次优化,动态规划。求解:productArray[i][j]的时候不用再次循环从i到j。而是利用:productArray[i][j]=productArray[i][j-1]*A[j];採用递推的方法来计算,算法时间复杂度为O(n2),遗憾的是也TLE了。
public class Solution {
public int maxProduct(int A[]) {
if(A==null||A.length==0) {
return 0;
}
int[][] productArray = new int[A.length][A.length];
int maxProduct = A[0];
for(int i=0;i<A.length;i++) {
for(int j=i;j<A.length;j++) {
if(j==i) {
productArray[i][i] = A[i];
} else {
productArray[i][j] = productArray[i][j-1]*A[j];
}
if(productArray[i][j]>maxProduct) {
maxProduct = productArray[i][i];
}
}
}
return maxProduct;
}
}
第二次优化。事实上子数组乘积最大值的可能性为:累乘的最大值碰到了一个正数;或者。累乘的最小值(负数),碰到了一个负数。所以每次要保存累乘的最大(正数)和最小值(负数)。同一时候另一个选择起点的逻辑。假设之前的最大和最小值同当前元素相乘之后,没有当前元素大(或小)那么当前元素就可作为新的起点。比如,前一个元素为0的情况,{1,0,9,2}。到9的时候9应该作为一个最大值,也就是新的起点。{1,0,-9,-2}也是相同道理,-9比当前最小值还小,所以更新为当前最小值。
这样的方法仅仅须要遍历一次数组就可以,算法时间复杂度为O(n)。
public class Solution {
public int maxProduct(int A[]) {
if(A==null||A.length==0) {
return 0;
}
int maxProduct = A[0];
int max_temp = A[0];
int min_temp = A[0];
for(int i=1;i<A.length;i++) {
int a = A[i]*max_temp;
int b = A[i]*min_temp;
max_temp = Math.max(Math.max(a,b), A[i]);
min_temp = Math.min(Math.min(a,b), A[i]);
maxProduct = Math.max(maxProduct, max_temp);
}
return maxProduct;
}
}
LeetCode Maximum Product Subarray 解题报告的更多相关文章
- 【LeetCode】152. Maximum Product Subarray 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 动态规划 参考资料 日期 题目地址:htt ...
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- 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 求最大子数组乘积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode]Maximum Product Subarray @ Python
原题地址:https://oj.leetcode.com/problems/maximum-product-subarray/ 解题思路:主要需要考虑负负得正这种情况,比如之前的最小值是一个负数,再乘 ...
- Leetcode Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 152.[LeetCode] Maximum Product Subarray
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- [LeetCode] Maximum Product Subarray 连续数列最大积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode Maximum Product Subarray 最大子序列积
题意:给一个size大于0的序列,求最大的连续子序列之积.(有正数,负数,0) 思路:正确分析这三种数.0把不同的可能为答案的子序列给隔开了,所以其实可以以0为分隔线将他们拆成多个序列来进行求积,这样 ...
随机推荐
- java学习第一阶段——面向对象
你聪明有人会说你心机重, 你靠的是努力有人会说你运气好, 你说自己天生乐观有人会说你虚假, 有时候, 你明明就是一杯白水, 却被人硬生生逼成了满肚子憋屈的碳酸饮料. 人一生要遇见太多人, 即使有些话字 ...
- 跟我学android-Notification
Notification 可以理解为通知的意思,会出现在通知栏,比如来了一条短信 使用 Notification 有以下3个步骤: 1. 创建 NotificationManager的对象 2.为No ...
- 用core dump来调试程序段错误
有的程序可以通过编译, 但在运行时会出现Segment fault(段错误). 这通常都是指针错误引起的.但这不像编译错误一样会提示到文件->行, 而是没有任何信息, 使得我们的调试变得困难起来 ...
- 安装 php-gd
安装cmd提示不支持GD模块,不能使用缩略图功能,需要安装php-gd 1.安装 php-gd yum list php-gd yum install php-gd 2.重启 httpd servic ...
- @font-face扒站的步骤
今天模仿百度首页手机版的时候遇到的@font-face的问题,现在整理一下. 问题:图中红色区域,在拷贝F12样式的时候,并没有出现这些小图标. 图1:百度的效果 ...
- IE6的bug
借鉴http://css.doyoe.com/ 问题和经验列表,里面应有尽有.写几个常见的: 1.解决IE6及更早浏览器浮动时产生双倍边距的BUG display:inline 2.如何解决IE6下的 ...
- JQUERY1.9学习笔记 之基本过滤器(八) 最后元素选择器
最后元素选择器 jQuery( ":last" ) 描述:选择与之匹配的最后元素. 例:选择表格的最后一行. <!DOCTYPE html><html lang= ...
- phpMemcache消息队列类
<?php /** * Memcache 消息队列类 */ class QMC { const PREFIX = 'ASDFASDFFWQKE'; /** * 初始化 mc * @staticv ...
- sql 语句左连接右连接小例子
A表(a1,b1,c1) B表(a2,b2) a1 b1 c1 a2 b2 01 数学 95 01 张三 02 语文 90 02 李四 03 英语 80 04 王五 select A.*,B.* fr ...
- 个人Python常用Package及其安装
为了避免每次重装系统时又要东翻西找,现在此记录一下目前常用的Python包安装过程. 1) Python: 2.7.11, 下载地址:www.python.org.由于个人喜欢使用PyQt4(其实是不 ...