[leetCode][001] Maximum Product Subarray
题目:
Find the contiguous subarray within an array (containing at least one number) which has the largest product.
For example, given the array
[2,3,-2,4],
the contiguous subarray[2,3]has the largest product =6.
题意:
题目中给出一个(至少包含一个元素)整形数组,求一个子数组(元素连续),使得其元素之积最大。
最直接了当的方法,当然是暴力穷举,但是其O(n^2)是无法通过LeetCode的OJ的。
以下给出了本题的解决方法,O(n)时间复杂度,C++实现。
class Solution {
public:
int maxProduct(int A[], int n) {
int nMaxPro = A[];
int max_tmp = A[];
int min_tmp = A[];
for(int i = ; i< n; ++i){
int a = A[i] * max_tmp;
int b = A[i] * min_tmp;
max_tmp = (( a > b ? a : b ) > A[i]) ? ( a > b ? a : b ) : A[i];
min_tmp = (( a < b ? a : b ) < A[i]) ? ( a < b ? a : b ) : A[i];
nMaxPro = (nMaxPro > max_tmp) ? nMaxPro : max_tmp;
}
return nMaxPro;
}
};
运行结果:

希望各位看官不吝赐教,小弟感谢~!
[leetCode][001] Maximum Product Subarray的更多相关文章
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- [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之Maximum Product Subarray
1.(原文)问题描述 Find the contiguous subarray within an array (containing at least one number) which has t ...
- [leetcode]152. Maximum Product Subarray最大乘积子数组
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
随机推荐
- ssh面试题
ssh面试题 创建时间: 2015-8-12 22:37 来源: http://wenku.baidu.com/link?url=cw1B46f98hAde0kmr3J-wv7PpklZJRmf6I ...
- django动态表格总结
应用场景: A与B之间存在一对多关系. CBV实现方案: CreateView/UpdateView + inlineformset + jquery 具体: view方面:重写post/get方法, ...
- Java和C#运行速度对比:Java比C#快约3倍
测试条件: Java版本: Java 8, .NET版本:v4.0, Release模式,针对x86平台优化 单线程模式. 测试1:(测试1的代码摘自http://blog.csdn.net/houj ...
- Jquery获取数据并生成下拉菜单
<script type="text/javascript"> $(document).ready(function() { GetByJquery(); $(&quo ...
- 2013 ACM/ICPC 长春网络赛E题
题意:给出一个字符串,要从头.尾和中间找出三个完全相等的子串,这些串覆盖的区间互相不能有重叠部分.头.尾的串即为整个字符串的前缀和后缀.问这个相同的子串的最大长度是多少. 分析:利用KMP算法中的ne ...
- iOS7 status bar 样式问题
在ios7中,有如下status bar 样式 typedef NS_ENUM(NSInteger, UIStatusBarStyle) { UIStatusBarStyleDefault = , / ...
- maven的一些依赖
maven的一些依赖: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://w ...
- mybatis There is no getter for property named 'xx' in 'class java.lang.String
转载自://http://www.cnblogs.com/anee/p/3324140.html 用mybatis查询时,传入一个字符串传参数,且进行判断时,会报 There is no getter ...
- HDU 5793 A Boring Question (逆元+快速幂+费马小定理) ---2016杭电多校联合第六场
A Boring Question Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- VS禁止特定警告
项目右键-属性-配置属性-C/C++-高级-禁用特定警告-填写警告编号例如4996