Maximum Product Subarray
来源:互联网 发布:网络黑白txt 花无涯 编辑:程序博客网 时间:2024/05/16 00:47
source: http://blog.csdn.net/sbitswc/article/details/39546719
Analysis:
similar like Maximum Subarray question
difference is the max value could be get from 3 situations
current maxValue * A[i] if A[i]>0
current minValue * A[i] if A[i]<0
A[i]
We need to record current maxValue, current minValue and update them every time get the new product
class Solution {public: int maxProduct(int A[], int n) { if(n <= 0) return 0; if(n == 1) return A[0]; int curMin = A[0]; int curMax = A[0]; int ans = A[0]; for(int i=1; i<n; i++) { int temp1 = A[i] * curMin; int temp2 = A[i] * curMax; curMin = min(A[i], min(temp1, temp2)); curMax = max(A[i], max(temp1, temp2)); ans = max(curMax, ans); } return ans; }};
0 0
- LeetCode_Maximum Subarray | Maximum Product Subarray
- Maximum Product Subarray
- Maximum Product Subarray
- Maximum Product Subarray 【LeetCode】
- [LeetCode] Maximum Product Subarray
- leetcode-Maximum Product Subarray
- Maximum Product Subarray -- LeetCode
- LeetCode-Maximum Product Subarray
- [LeetCode]Maximum Product Subarray
- [LeetCode]Maximum Product Subarray
- 【LeetCode】Maximum Product Subarray
- leetcode:Maximum Product Subarray
- leetcode: Maximum Product Subarray
- Leetcode: Maximum Product Subarray
- Maximum Product Subarray
- Maximum Product Subarray
- [LeetCode]Maximum Product Subarray
- Leetcode Maximum Product Subarray
- 不懂老公的手机密码怎么办?QQ1622863529
- 手机短信删除了还能恢复吗?QQ1622863529
- Android,widget
- 获取手机第三方应用的信息(名称,包名,版本号,版本名,安装包路径,安装包大小)
- Java 读取Oracle数据库中的Date日期型怎么去掉秒后面的0
- Maximum Product Subarray
- 不知道别人的QQ密码怎么关联QQ?QQ1622863529
- LoadRunner的上传及下载脚本
- 【Hibernate】把Hibernate在后台查询到的数据通过Struts标签在前台进行输出
- 不懂别人的陌陌密码怎么登录?QQ1622863529
- 纠结
- 怎么盗别人的微信?QQ1622863529
- Hadoop Mapreduce优先级调度
- 手机通话记录该怎么查?QQ1622863529