152. Maximum Product Subarray - LeetCode
Question

Solution
题目大意:求数列中连续子序列的最大连乘积
思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的,这个问题要注意正负,需要维护两个结果
Java实现:
public int maxProduct(int[] nums) {
if (nums.length == 1) return nums[0];
// 定义问题:状态及对状态的定义
// 设max[i]表示数列中第i项结尾的连续子序列的最大连乘积
// 求max[0]...max[n]中的最大值
// 状态转移方程
// max[0] = nums[0]
// max[i] = Max.max(max[i-1] * nums[i], nums[i])
int[] max = new int[nums.length];
int[] min = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
max[i] = min[i] = nums[i];
}
int product = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] < 0) {
max[i] = Math.max(min[i - 1] * nums[i], max[i]);
min[i] = Math.min(max[i - 1] * nums[i], min[i]);
product = Math.max(max[i], product);
} else {
max[i] = Math.max(max[i - 1] * nums[i], max[i]);
min[i] = Math.min(min[i - 1] * nums[i], min[i]);
product = Math.max(max[i], product);
}
}
return product;
}
int maxProduct(int A[], int n) {
// store the result that is the max we have found so far
int r = A[0];
// imax/imin stores the max/min product of
// subarray that ends with the current number A[i]
for (int i = 1, imax = r, imin = r; i < n; i++) {
// multiplied by a negative makes big number smaller, small number bigger
// so we redefine the extremums by swapping them
if (A[i] < 0)
swap(imax, imin);
// max/min product for the current number is either the current number itself
// or the max/min by the previous number times the current one
imax = max(A[i], imax * A[i]);
imin = min(A[i], imin * A[i]);
// the newly computed max value is a candidate for our global result
r = max(r, imax);
}
return r;
}
关于动态规划

Ref
152. Maximum Product Subarray - LeetCode的更多相关文章
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- 求连续最大子序列积 - 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 (最大乘积子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode]152. Maximum Product Subarray
This a task that asks u to compute the maximum product from a continue subarray. However, you need t ...
- Maximum Product Subarray——LeetCode
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【LeetCode】152. Maximum Product Subarray 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双重循环 动态规划 参考资料 日期 题目地址:htt ...
- Java for LeetCode 152 Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- 微信小程序 iphone6 和 iphone6plus 如何设置rpx单位,通俗易懂的方法
pt:屏幕物理像素(屏幕实际宽度像素) px:屏幕分辨率 pt和px关系:iphone6plusppi密度高,1pt里有3px,iphone6 1pt里有2px. iphone6宽度 (物理像素) : ...
- react开发教程(三)组件的构建
什么是组件 组件化就好像我们的电脑装机一样,一个电脑由显示器.主板.内存.显卡.硬盘,键盘,鼠标.... 组件化开发有如下的好处:降低整个系统的耦合度,在保持接口不变的情况下,我们可以替换不同的组件快 ...
- WebGL2系列之顶点数组对象
使用了顶点缓冲技术后,绘制效率有了较大的提升.但是还有一点不尽如人意,那就是顶点的位置坐标.法向量.纹理坐标等不同方面的数据每次使用时需要单独指定,重复了一些不必要的工作.WebGL2提供了一种专门用 ...
- java中到底什么是继承?
1.何为继承?What is Inheritance? 在上图中,对于车来讲,汽车就是子类.对于汽车来讲,奔驰就是子类.车是汽车的基类,超类,或说父类.到底什么是继承?马克-to-win,子类把父类的 ...
- Java 值传递 or 引用传递?
Java 方法传参 值传递 or 引用传递? 结论:Java采用的是值传递 先建立一些基础的概念 什么是值传递和引用传递? 值传递(pass by value):是指在调用函数时将实际参数复制一份传递 ...
- 不同标准下的C语言常量范围的默认类型的检测 (测试样例为C90与C99)
不同标准下的C语言常量范围的默认类型的检测 一.C90与C99标准下的不同常量范围的默认类型 C90标准下对不同常量范围默认类型的检测实现及运行结果: C99标准下对不同范围默认类型的检测实现 ...
- SLF4J 报错解决:No SLF4J providers were found
1.解决SLF4J报错 我们在自己的项目中使用了SLF4J,或者引入了某开源项目时,他的项目中用了SLF4J,运行时会报如下的错误: SLF4J: Failed to load class " ...
- Python入门-安装Python开发环境
1.安装开发环境 #方法一:直接安装anaconda,解释器和环境,一个软件就可以包括,简单方便 参考地址:https://www.cnblogs.com/sui776265233/p/1145300 ...
- Ubuntu16.04 安装和卸载MySQL数据库
Ubuntu16.04 安装和卸载MySQL数据库 1 安装 安装非常简单,只需要三个命令 1.1 安装服务端 sudo apt-get install mysql-server 在这一步过程中会有提 ...
- 基于Nginx实现反向代理
一.nginx的简介 Nginx 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务 其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服 ...