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 解题报告的更多相关文章

  1. 【LeetCode】152. Maximum Product Subarray 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 动态规划 参考资料 日期 题目地址:htt ...

  2. LeetCode Maximum Product Subarray(枚举)

    LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...

  3. LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关

    Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...

  4. [LeetCode] Maximum Product Subarray 求最大子数组乘积

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  5. [leetcode]Maximum Product Subarray @ Python

    原题地址:https://oj.leetcode.com/problems/maximum-product-subarray/ 解题思路:主要需要考虑负负得正这种情况,比如之前的最小值是一个负数,再乘 ...

  6. Leetcode Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  7. 152.[LeetCode] Maximum Product Subarray

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  8. [LeetCode] Maximum Product Subarray 连续数列最大积

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  9. LeetCode Maximum Product Subarray 最大子序列积

    题意:给一个size大于0的序列,求最大的连续子序列之积.(有正数,负数,0) 思路:正确分析这三种数.0把不同的可能为答案的子序列给隔开了,所以其实可以以0为分隔线将他们拆成多个序列来进行求积,这样 ...

随机推荐

  1. 开始学习<p>标签,添加段落

    如果想在网页上显示文章,这时就需要<p>标签了,把文章的段落放到<p>标签中. 语法: <p>段落文本</p> 注意一段文字一个<p>标签, ...

  2. Android热更新开源项目Tinker集成实践总结

    前言 最近项目集成了Tinker,开始认为集成会比较简单,但是在实际操作的过程中还是遇到了一些问题,本文就会介绍在集成过程大家基本会遇到的主要问题. 考虑一:后台的选取 目前后台功能可以通过三种方式实 ...

  3. 简单总结焦点事件、Event事件对象、冒泡事件

    每学习一些新的东西,要学会复习,总结和记录. 今天来简单总结一下学到的几个事件:焦点事件.Event事件对象.冒泡事件 其实这几个事件应该往深的说是挺难的,但今天主要是以一个小菜的角度去尝试理解一些基 ...

  4. How to check a not defined variable in javascript

    javascript里怎么检查一个未定义的变量? in JavaScript null is an object. There's another value for things that don' ...

  5. js 获取页面高度和宽度(兼容 ie firefox chrome),获取鼠标点击位置

    <script> //得到页面高度 var yScroll = (document.documentElement.scrollHeight >document.documentEl ...

  6. Nginx源码研究四:NGINX的内存管理

    关于nginx的内存使用,我们先看代码,下面是nginx_cycle.c中对全局数据结构cycle的初始化过程 pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, ...

  7. Win7-IIS7下运行PHP网站(以配置好的drupal网站为例)

    0.前提:IIS7已启用. drupal网站配置文件web.config中用到了“简洁链接”(URL重写),所以,还需要事先安装URL重写模块. URL重写模块(url rewrite)下载地址: r ...

  8. SCJP_104——题目分析(4)

    14. which three are valid declaraction of a float? ADFA. float foo=-1; B. float foo=1.0; C. float fo ...

  9. 利用WebApi获取手机号码归属地

    前述: 在WebApi中,涉及到一个重要的类,HttpWebRequest. 学习link:httpwebrequest详解 示例演示: 代码示例: 1.前端代码: @{ ViewBag.Title ...

  10. traceroute命令

    traceroute指令让你追踪网络数据包的路由途径,预设数据包大小是40Bytes,用户可另行设置. 通过traceroute我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径.当然每次 ...