[leetcode]152. Maximum Product Subarray最大乘积子数组
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.
题意:
求最大乘积子数组
思路:
1. the sign(I mean, if it is positive or negative) influence the product value
2. when iterating each item, it can be postive or negative
3. we use max[i] to stand for max product of ith item
4. we use min[i] to stand for min product of ith item
5. so function rule:
max[i]: max(max[i -1]*nums[i], min[i-1]* nums[i], nums[i])
min[i]: min(max[i -1]*nums[i], min[i-1]* nums[i], nums[i])




code
public int maxProduct(int[] nums) {
// initialize
int[] max = new int[nums.length];
int[] min = new int[nums.length];
max[0] = nums[0];
min[0] = nums[0];
int result = nums[0];
// max[i]: max product of ith item
// min[i]: min product of ith item
for(int i = 1; i < nums.length; i++){
max[i] = Math.max(Math.max(max[i-1] * nums[i] , min[i-1]*nums[i]), nums[i]);
min[i] = Math.min(Math.min(max[i-1] * nums[i] , min[i-1]*nums[i]), nums[i]);
result = Math.max(result, max[i]);
}
return result;
}
[leetcode]152. Maximum Product Subarray最大乘积子数组的更多相关文章
- [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- LeetCode 152. Maximum Product Subarray (最大乘积子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- C#解leetcode 152. Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- Java for LeetCode 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 --------- java
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 152. Maximum Product Subarray最大乘积子数组/是否连续
[抄题]: Given an integer array nums, find the contiguous subarray within an array (containing at least ...
- Leetcode#152 Maximum Product Subarray
原题地址 简单动态规划,跟最大子串和类似. 一维状态空间可以经过压缩变成常数空间. 代码: int maxProduct(int A[], int n) { ) ; ]; ]; ]; ; i > ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
随机推荐
- 生产redis client 连接无法释放
原因是spring配置文件,开启了事务导致的,redis是缓存用的,不需要开启事务,正确的配置如下: <!--redis操作模版,使用该对象可以操作redis --> <bean i ...
- PHPSTORM ACTIVATION 注册激活
最近新出了PHPSTORM10,于是把自己机器上的升级了下.这家伙收费的,国人嘛...你懂的. 安装后,发现不能用原来的keygen注册激活了,于是Google了一下,下面是解决方案: 安装好打开的时 ...
- SQL 查询当天,本月,本周的记录
SELECT * FROM 表 WHERE CONVERT(Nvarchar, dateandtime, 111) = CONVERT(Nvarchar, GETDATE(), 111) ORDE ...
- centos查看系统版本信息
1.查看版本文件名称 ll /etc/*centos* 2.显示系统版本号 cat /etc/centos-release
- Python完全新手教程
转发:作者: taowen 来源: 博客园 发布时间: 2010-10-01 00:42 阅读: 1618 次 推荐: 0 原文链接 [收藏] Lesson ...
- vagrant up报错 Warning: Authentication failure. Retrying...解决方案
参照链接 https://www.cnblogs.com/zqifa/p/vagrant-1.html 可以解决问题.
- Python的xml模块
先来一段xml代码 <?xml version="1.0"?> <data> <country name="Liechtenstein&qu ...
- Failed to resolve: common Open File 导入项目问题
Failed to resolve: common Open File Warning:Configuration 'compile' is obsolete and has been replac ...
- weex Mac创建项目
序言:本来在win 10 上创建项目真的很顺利!后来入手一个mac就从mac 上下载了最新的android studio开始搞起了weex,问题来了,weex-toolkit脚手架还是老的,我觉得是w ...
- Node Express服务器设置与优化
一.代码部分 * 启用gzip压缩,减少网络数据量 var compression = require('compression')var express = require('express')va ...