Leetcode#152 Maximum Product Subarray
简单动态规划,跟最大子串和类似。
一维状态空间可以经过压缩变成常数空间。
代码:
int maxProduct(int A[], int n) {
if (n <= )
return ;
int res = A[n - ];
int minp = A[n - ];
int maxp = A[n - ];
for (int i = n - ; i >= ; i--) {
int tmp = minp;
minp = min(A[i], min(A[i] * minp, A[i] * maxp));
maxp = max(A[i], max(A[i] * tmp, A[i] * maxp));
res = max(res, maxp);
}
return res;
}
Leetcode#152 Maximum Product Subarray的更多相关文章
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- [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 ...
- 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 ...
- C#解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最大乘积子数组
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- 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 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
随机推荐
- 线程间通信--wait和notify
使用wait.notify方法实现线程间的通信(注意这两个方法都是object的类的方法,换句话说java为所有的对象都提供了这两个方法) 1.wait和notify必须配合synchronized关 ...
- ajax+php+mysql更新
html代码 <input type="button" id="quxiao" class="quxiao" name="q ...
- Microsoft Visual C++ Runtime error解决方法
1: 当出现下图时提示Microsoft Visual C++ Runtime error 2:此时不要关闭该对话框,然后打开任务管理器(Ctrl+Shift+Esc)如下图: 找到Microsoft ...
- VMware虚拟机升级过程中遇到的一点问题
在将VWware由9.0升级到10.0的过程中,出现如下图的错误: failed to create the requested registry key Key:Installer e ...
- How to create Lookup Worker Filtered by Legal Entity[AX2012]
1. Add a new method in hcmWorker table, and add this script : public static client void lookupWorker ...
- 压力测试之TCPP
1.下载源码 tpcc-mysql-src.tgz 2.解压 tpcc-mysql-src.tgz 3.安装 [root@DBMysql mysql]# cd /home/mysql/tpcc-mys ...
- 在linux下安装Mongodb
1.到官网下载源码:mongodb-linux-x86_64-rhel55-3.2.4.gz 2.安装 创建用户组.用户.目录 [root@hadoop1 ~]# groupadd mongodb [ ...
- .Net 自己写个简单的 半 ORM (练手)
ORM 大家都知道, .Net 是EF 还有一些其他的ORM 从JAVA 中移植过来的 有 , 大神自己写的也有 不管ORM 提供什么附加的 乱七八糟的功能 但是 最主要的 还是 关系映射 的事情 ...
- python 逻辑运算符与比较运算符的差别
文章内容摘自:http://www.cnblogs.com/vamei/archive/2012/05/29/2524376.html 逻辑运算符 and, or, not 比较运算符 ==, !=, ...
- ios category
https://github.com/shaojiankui/IOS-Categories