Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product.

Example 1:

Input: [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.

Example 2:

Input: [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
class Solution {
public int maxProduct(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int max = nums[0];
int min = nums[0];
int res = nums[0];
int preMax = max;
for (int i = 1; i < nums.length; i++) {
// include the current for comparison
max = Math.max(nums[i], Math.max(preMax * nums[i], min * nums[i]));
min = Math.min(nums[i], Math.min(min * nums[i], preMax * nums[i]));
res = Math.max(max, res);
preMax = max;
}
return res;
}
}

[LC] 152. Maximum Product Subarray的更多相关文章

  1. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

  2. 152. Maximum Product Subarray - LeetCode

    Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...

  3. 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray

    题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...

  4. 【刷题-LeetCode】152 Maximum Product Subarray

    Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...

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

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

  6. [LeetCode]152. Maximum Product Subarray

    This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...

  7. LeetCode 152. Maximum Product Subarray (最大乘积子数组)

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

  8. 152. Maximum Product Subarray最大乘积子数组/是否连续

    [抄题]: Given an integer array nums, find the contiguous subarray within an array (containing at least ...

  9. 152. Maximum Product Subarray (Array; DP)

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

随机推荐

  1. Res-net 标准版本源码差异-官方源码示例

    # resnet https://github.com/tensorflow/models/blob/master/research/slim/nets/resnet_v1.py https://gi ...

  2. Thread--lock,lockInterruptibly,tryLock,tryLock(long timeout, TimeUnit unit)

    参考:http://www.dewen.net.cn/q/9077 http://coolxing.iteye.com/blog/1236909 lock,tryLock,lockInterrupti ...

  3. 1. laravel 学习 环境搭建

    1. 项目环境 vagrant + laradock  (因为 自己手动搭建环境太麻烦了 自己弄了一下 感觉还是有些漏洞 所以采用 laradock) 2. Vagrantfile 备注 : box  ...

  4. 吴裕雄--天生自然Linux操作系统:Linux 忘记密码解决方法

    忘记Linux系统的root密码,linux系统忘记root密码的情况该怎么办呢?重新安装系统吗?当然不用!进入单用户模式更改一下root密码即可. 步骤如下: 重启linux系统 3 秒之内要按一下 ...

  5. goweb-访问数据库

    访问数据库 对许多Web应用程序而言,数据库都是其核心所在.数据库几乎可以用来存储你想查询和修改的任何信息,比如用户信息.产品目录或者新闻列表等. Go没有内置的驱动支持任何的数据库,但是Go定义了d ...

  6. Python——课程数据统计分析

    介绍 在该章节中我们将利用提供的课程数据来进行一次实战性质的时间序列和聚类分析. 知识点 数据处理 数据可视化 中文分词 文本聚类 数据概览 本次课程的数据来源于运行过程中产生的真实数据,我们对部分数 ...

  7. visual studio2019下静态链接库的制作

    创建静态库项目 项目名称为20199324lib // pch.h #ifndef __PCH__ #define __PCH__ extern int add(int a, int b);//ext ...

  8. 893B. Beautiful Divisors#美丽的因子(打表法)

    题目出处:http://codeforces.com/problemset/problem/893/B 题目大意:找到一个数在二进制下,最大的以k个连续的1和k-1个连续的0组成的数字作为因子 #in ...

  9. python程序的打开运行方式

    python程序的运行方式大致可以分为两种,一种是直接通过python解释器直接解释型运行,另外一种是先把python程序编译为二进制文件再运行. .源代码 -python源代码的文件以"p ...

  10. android课程表控件、悬浮窗、Todo应用、MVP框架、Kotlin完整项目源码

    Android精选源码 Android游戏2048 MVP Kotlin项目(RxJava+Rerotfit+OkHttp+Glide) Android基于自定义Span的富文本编辑器 android ...