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 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
随机推荐
- (function ( ){})( ) 与 (function ( ){}( )) 有什么区别?
js立即执行函数: (function ( ){})( ) 与 (function ( ){}( )) 有什么区别? 转自:http://www.jb51.net/article/75089.htm ...
- (原创)Windows8下安装配置WAMP
Windows8下安装配置WAMP 2013/12/28 最近这段时间一直在研究linuxshell编程,虽然还是初级水平,但比之前有了不小的进度,但是shell的命令很多,很难在短时间 ...
- C++判断五位以内的对称素数
题目内容:判断一个数是否为对称且不大于五位数的素数. 输入描述:输入数据含有不多于50个的正整数n(0<n<232). 输出描述:对于每个n,如果该数是不大于五位数的对称素数,则输出“Ye ...
- Google搜索镜像
From:http://www.cnblogs.com/killerlegend/p/3783744.html Date:2014.6.12 By KillerLegend Google 搜索:htt ...
- 关于 mvc 中 连字符 - 和下划线 _转换的问题。
[潜水]大崔||哈尔滨(759666247) 10:02:16 如图 C#不承认 “-”[知府]古道今-湖北\xig<systemobject@126.com> 10:03:54 ...
- Learning Scrapy笔记(五)- Scrapy登录网站
摘要:介绍了使用Scrapy登录简单网站的流程,不涉及验证码破解 简单登录 很多时候,你都会发现你需要爬取数据的网站都有一个登录机制,大多数情况下,都要求你输入正确的用户名和密码.现在就模拟这种情况, ...
- JForum二次开发(一)
1.环境 myeclipse2014,jdk7,tomcat8,mysql5.6 2.下载源码地址 http://jforum.net/download.jsp 3.导入源码 新建web工程JForu ...
- afddaf
//import javax.swing.*; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JL ...
- SQLite判断某表是否存在
SQLite判断表是否存在:其实很简单,只要查看sqlite_master表中是否存在这条数据就可以知道了.SELECT count(*) FROM sqlite_master WHERE type= ...
- kettle发送邮件
使用kettle发送邮件是为了更好的监控ETL的加载信息 以下是我通过测试的一个案例 1. JOB示意图 2.邮件发送配置详细信息 2.1地址信息配置 2.2 服务器信息配置 上图中所说的" ...