找出一个序列中乘积最大的连续子序列(该序列至少包含一个数)。
例如, 给定序列 [2,3,-2,4],
其中乘积最大的子序列为 [2,3] 其乘积为 6。
详见:https://leetcode.com/problems/maximum-product-subarray/description/

Java实现:

方法一:

用两个dp数组,其中f[i]表示子数组[0, i]范围内并且一定包含nums[i]数字的最大子数组乘积,g[i]表示子数组[0, i]范围内并且一定包含nums[i]数字的最小子数组乘积,初始化时f[0]和g[0]都初始化为nums[0],其余都初始化为0。那么从数组的第二个数字开始遍历,那么此时的最大值和最小值只会在f[i-1]*nums[i]、g[i-1]*nums[i]和nums[i]这三个数字之间产生。所以我们用三者中的最大值来更新f[i],用最小值来更新g[i],然后用f[i]来更新结果res即可,由于最终的结果不一定会包括nums[n-1]这个数字,所以f[n-1]不一定是最终解,不断更新的结果res才是。

class Solution {
public int maxProduct(int[] nums) {
int n=nums.length;
int res=nums[0];
int[] f=new int[n];
int[] g=new int[n];
f[0]=nums[0];
g[0]=nums[0];
for(int i=1;i<n;++i){
f[i]=Math.max(Math.max(f[i-1]*nums[i],g[i-1]*nums[i]),nums[i]);
g[i]=Math.min(Math.min(g[i-1]*nums[i],f[i-1]*nums[i]),nums[i]);
res=Math.max(res,f[i]);
}
return res;
}
}

方法二:

class Solution {
public int maxProduct(int[] nums) {
int n=nums.length;
int res=nums[0];
int mn=nums[0];
int mx=nums[0];
for(int i=1;i<n;++i){
int tmax=mx,tmin=mn;
mx=Math.max(Math.max(tmax*nums[i],tmin*nums[i]),nums[i]);
mn=Math.min(Math.min(tmax*nums[i],tmin*nums[i]),nums[i]);
res=Math.max(res,mx);
}
return res;
}
}

参考:https://www.cnblogs.com/grandyang/p/4028713.html

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 (最大乘积子数组)

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

  7. [LeetCode]152. Maximum Product Subarray

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

  8. [leetcode]152. Maximum Product Subarray最大乘积子数组

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

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

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

随机推荐

  1. bash的pushd和popd

    1 pushd和popd是bash shell的builtin命令 2 pushd和popd维护了一个目录栈 pushd xxx就是将xxx放入目录栈顶. 目录栈顶就是当前的目录. 但是cd的话,会不 ...

  2. jquery 选择器(selector)和事件(events)

    页面加载完成后开始运行do stuff when DOM is ready 中的语句! $(document).ready(function() {       // do stuff when DO ...

  3. Spring boot 使用Junt

    //@RunWith:启动器,SpringJUnit4ClassRunner:Spring整合JUnit4 //@SpringBootTest获取启动类,相当于@Contextconfiguartio ...

  4. (linux)main.c中的初始化

    main.c中的初始化 head.s在最后部分调用main.c中的start_kernel() 函数,从而把控制权交给了它. 所以启动程序从start_kernel()函数继续执行.这个函数是main ...

  5. BootLoader与Linux内核的参数传递【转】

    本文转载自:http://blog.sina.com.cn/s/blog_476d8cf30100rttx.html 在嵌入式系统中,BootLoader 是用来初始化硬件,加载内核,传递参数.因为嵌 ...

  6. WAS域名解析问题

    1.如果dmgr和his在一台机器上,但web服务器用的是app服务器 如下图: 插件都处理完成. 这时候,通过外网域名访问时,出现如下情况 或者 说明:ihs服务器上webserver1的文件Plu ...

  7. Android的三种主流资源尺寸

    Android三种主流资源屏幕尺寸:QVGA.HVGA.WVGA VGA的分辨率是640x480像素 QVGA(Quarter VGA)就是320x240,即VGA分辨率的1/4 HVGA(Half ...

  8. Apache Maven的下载、安装、测试

    Apache Maven是个软件项目管理工具,基于项目对象模型(Project Object Model,POM)的概念,Maven可用来管理项目的依赖.编译.文档等信息. 使用Maven管理项目时, ...

  9. codeforces 696B B. Puzzles(树形dp+概率)

    题目链接: B. Puzzles time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  10. Win7系统打开服务管理界面的几种方法汇总

    转自:https://www.jb51.net/os/windows/318465.html Win7服务管理包含了计算机操作系统和应用程序提供的所有服务,但是这么多服务并非总是用户所需的.比如打印机 ...