152.Maximum Product Subarray---dp---连续子数组的最大乘积---《编程之美》2.13子数组的最大乘积
题目链接:https://leetcode.com/problems/maximum-product-subarray/description/
题目大意:给出一串数组,找出连续子数组中乘积最大的子数组的乘积。
法一:暴力。竟然能过,数据也太水了。两个for循环,遍历每一个可能的连续子数组,找出最大值。代码如下(耗时161ms):
public int maxProduct(int[] nums) {
int res = Integer.MIN_VALUE, len = nums.length;
for(int i = 0; i < len; i++) {
int sum = 1;
for(int j = i; j < len; j++) {
sum *= nums[j];
res = Math.max(res, sum);
}
}
return res;
}
法二(借鉴):dp,利用两个dp数组,dp_max[i]表示从0到i的数中,子数组乘积最大值,dp_min[i]表示从0到i的数中,子数组乘积最小值,然后每次都更新这两个值,从dp_max数组中取的最大值即可。代码如下(耗时4ms):
public int maxProduct(int[] nums) {
int dp_max[] = new int[nums.length];
int dp_min[] = new int[nums.length];
int res = Integer.MIN_VALUE;
dp_max[0] = nums[0];
dp_min[0] = nums[0];
for(int i = 1; i < nums.length; i++) {
//更新最大值数组
dp_max[i] = Math.max(Math.max(dp_max[i - 1] * nums[i], dp_min[i - 1] * nums[i]), nums[i]);
//更新最小值数组
dp_min[i] = Math.min(Math.min(dp_max[i - 1] * nums[i], dp_min[i - 1] * nums[i]), nums[i]);
res = Math.max(dp_max[i], res);
}
return res;
}
法三(借鉴):两次遍历,正向遍历+反向遍历,空间复杂度是o(1),遇0则归1,否则尽管相乘,找到最大值。代码如下(耗时1ms);
public int maxProduct(int[] nums) {
int res = Integer.MIN_VALUE, sum = 1;
//正向遍历,取最大值
for(int i = 0; i < nums.length; i++) {
sum *= nums[i];
res = Math.max(res, sum);
if(nums[i] == 0) {
sum = 1;
}
}
//反向遍历,取最大值
sum = 1;
for(int i = nums.length - 1; i >= 0; i--) {
sum *= nums[i];
res = Math.max(res, sum);
if(nums[i] == 0) {
sum = 1;
}
}
return res;
}
法四(借鉴):下面这种方法也是用两个变量来表示当前最大值和最小值的,但是没有无脑比较三个数,而是对于当前的nums[i]值进行了正负情况的讨论:
2. 当遍历到一个负数或0时,我们先用一个变量t保存之前的最大值mx,然后此时的最大值等于之前最小值乘以这个负数和当前负数中的较大值,此时的最小值等于之前保存的最大值t乘以这个负数和当前负数中的较小值。
3. 在每遍历完一个数时,都要更新最终的最大值。
public int maxProduct(int[] nums) {
int res = nums[0], ma = nums[0], mi = nums[0];
for(int i = 1; i < nums.length; i++) {
if(nums[i] > 0) {
ma = Math.max(ma * nums[i], nums[i]);
mi = Math.min(mi * nums[i], nums[i]);
}
//注意负数或0的情况
else {
int t = ma;
ma = Math.max(mi * nums[i], nums[i]);
mi = Math.min(t * nums[i], nums[i]);
}
res = Math.max(res, ma);
}
return res;
}
法五(借鉴):在上面的解法中我们分析了当nums[i]为正数时,最大值和最小值的更新情况,为负数时,稍有不同的就是最小值更新时要用到之前的最大值,而不是更新后的最大值,所以我们才要用变量t来保存之前的结果。而下面这种方法的巧妙处在于先判断一个当前数字是否是负数,是的话就交换最大值和最小值。那么此时的mx就是之前的mn,所以mx的更新还是跟上面的方法是统一的,而在在更新mn的时候,之前的mx已经保存到mn中了,而且并没有改变,所以可以直接拿来用。代码如下(耗时1ms):
public int maxProduct(int[] nums) {
int res = nums[0], ma = nums[0], mi = nums[0];
for(int i = 1; i < nums.length; i++) {
if(nums[i] <= 0) {
int t = ma;
ma = mi;
mi = t;
}
ma = Math.max(ma * nums[i], nums[i]);
mi = Math.min(mi * nums[i], nums[i]);
res = Math.max(ma, res);
}
return res;
}
152.Maximum Product Subarray---dp---连续子数组的最大乘积---《编程之美》2.13子数组的最大乘积的更多相关文章
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- 152. Maximum Product Subarray - LeetCode
Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- 【刷题-LeetCode】152 Maximum Product Subarray
Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array ( ...
- [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
This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...
- LeetCode OJ 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 (最大乘积子数组)
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 ...
随机推荐
- 【bzoj3774】最优选择 网络流最小割
题目描述 小N手上有一个N*M的方格图,控制某一个点要付出Aij的代价,然后某个点如果被控制了,或者他周围的所有点(上下左右)都被控制了,那么他就算是被选择了的.一个点如果被选择了,那么可以得到Bij ...
- 【BZOJ2806】Cheat(后缀自动机,二分答案,动态规划,单调队列)
[BZOJ2806]Cheat(后缀自动机,二分答案,动态规划,单调队列) 题面 BZOJ 洛谷 题解 很有趣的一道题啊 对于在所有的串上面进行匹配? 很明显的后缀自动机 所以先构建出广义后缀自动机 ...
- BZOJ 1070 修车 【费用流】
Description 同一时刻有N位车主带着他们的爱车来到了汽车维修中心.维修中心共有M位技术人员,不同的技术人员对不同 的车进行维修所用的时间是不同的.现在需要安排这M位技术人员所维修的车及顺序, ...
- 洛谷 P2261 [CQOI2007]余数求和 解题报告
P2261 [CQOI2007]余数求和 题意: 求\(G(n,k)=\sum_{i=1}^n k \ mod \ i\) 数据范围: \(1 \le n,k \le 10^9\) \(G(n,k)\ ...
- Hive(三)hive的高级操作
一.hive的各种join操作 语法结构:join_table:table_reference JOIN table_factor [join_condition]| table_reference ...
- 埃及分数&&The Rotation Game&&骑士精神——IDA*
IDA*:非常好用的搜索,可以解决很多深度浅,但是规模大的搜索问题. 估价函数设计思路:观察一步最多能向答案靠近多少. 埃及分数 题目大意: 给出一个分数,由分子a 和分母b 构成,现在要你分解成一系 ...
- Linux之进程通信20160720
好久没更新了,今天主要说一下Linux的进程通信,后续Linux方面的更新应该会变缓,因为最近在看Java和安卓方面的知识,后续会根据学习成果不断分享更新Java和安卓的方面的知识~ Linux进程通 ...
- Qt ------ QElapsedTimer 计算消耗多少时间
The QElapsedTimer class provides a fast way to calculate elapsed times. The QElapsedTimer class is u ...
- GIT每次都要输入用户名和密码的解决方案
三.配置客户端长期存储用户各和密码 长期存储密码: git config --global credential.helper store 缓存用户信息 3600s zb@zb-computer:/h ...
- ZOJ 3776 A - Pokemon Master 签到水题
link 求和比大小... /** @Date : 2017-03-23-21.26 * @Author : Lweleth (SoungEarlf@gmail.com) * @Link : http ...