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为分隔线将他们拆成多个序列来进行求积,这样 ...
随机推荐
- .net处理页面的抓取数据
//要抓取数据的页面路径 string url = "http://www.scedu.net/banshi/used-car/lower-secondary-education/middl ...
- IIS支持APK/ISO文件下载的方法
默认把安卓手机应用或游戏的apk格式文件上传到服务器空间是不能直接下载的,这是因为IIS的默认MIME类型中没有.apk文件,所以无法下载.@VCOO 既然.apk无法下载是因为没有MIME,那么添加 ...
- Swift - 29 - 参数的默认值
// 参数设置了默认值之后, 在调用的时候, 可以写这个参数 // 在参数前面添加"_", 表示取消外部参数名, 但还是建议使用苹果默认格式 func sayHello(nickN ...
- 微信返回上一页,页面中的AJAX的请求,对Get请求无效的解决办法
问题产生原因 最近在做一个微信的项目时,遇到一种很常见的情况,需求是这样的,当用户进入到"我的个人中心"的时候,会有一个点击跳转填写认证资料的按钮,点击此按钮后,会跳转 ...
- sencha app build 到 Capturing theme image不执行
解决sencha app build 到 Capturing theme image不执行 今天电脑重装系统,重新安装了sencha cmd,但是在打包时,到了 Capturing theme ima ...
- JavaScript_数组
/** * 数组本身也是对象 * js中数组类似于java里的map容器 长度可随意改变 ,元素类型任意 * */ // var arr = new Array(); // var arr = [1, ...
- 多个显示器, window.open的定位
// Pops a window relative to the current window position function popup(url, winName, xOffset, yOffs ...
- java获取天气信息
通过天气信息接口获取天气信息,首先要给项目导入程序所需要的包,具体需要如下几个包: json-lib-2.4.jar ezmorph-1.0.6.jar commons-beanutils-1.8.3 ...
- PHP开发套件
Windows系统下开发 环境配置: PHPstudy----立即下载 开发工具: PHPstorm----立即下载 引用一个注册服务器地址:潘田--phpstorm 2016.1注册码 当然推荐大家 ...
- mysql的sql优化案例
前言 mysql的sql优化器比较弱,选择执行计划貌似很随机. 案例 一.表结构说明mysql> show create table table_order\G***************** ...