[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 ...
随机推荐
- Decompiled .class file,bytecode version:51.0(Java 7) Source for 'Android API 23 Platform' not found
今天在Android Studio中访问Java源码的时候,代码上方出现如下提示: 而且方法体中显示介样内容: throw new RuntimeException("Stub!" ...
- 经典的SQL面试题
SQL中 inner join. left join .right join. outer join之间的区别 A表(a1,b1,c1) B表(a2,b2) a1 b1 c1 a2 b2 01 数学 ...
- MYSQL索引失效的各种情形总结
1) 没有查询条件,或者查询条件没有建立索引 2) 在查询条件上没有使用引导列 3) 查询的数量是大表的大部分,应该是30%以上. 4) 索引本身失效 5) 查询条件使用函数在索引列上,或者对索 ...
- phpmyadmin 主机名自动补全
2015年2月9日 14:29:25 新安装的phpmyadmin在登录界面中, 主机名的输入框没有自动补全功能, 导致每次都得手动输入ip地址 找到 phpmyadmin/libraries/aut ...
- C标签判断两个值是否相等
c标签判断两个值是否相等 Integer用:${user1.id eq user2.id}:int用:${user1.id == user2.id} 测试代码如下:<c:if test=&quo ...
- 教官的游戏(codevs 2793)
题目描述 Description 有N个学生刚吃完饭,准备出食堂. 国防学校有个规矩:必须2人一排或3人一列离开. 两个教官A,B轮流取2或3人,谁先取完谁就赢得游戏.(A先取) 若两人都用最优策略, ...
- Web性能测试中的几个关键指标
系统吞吐量 吞吐量指单位时间内系统处理的请求数量,体现系统的整体处理能力.对于网站,可以用"请求数/秒"或是"页面数/秒"来衡量,也可以用"访问人数/ ...
- MySQL常用简单小命令
1. 登录数据库: mysql -h localhost -u root -p
- mysql中char,varchar,text区别总结
具体对这三种类型的说明不做阐述可以查看mysql帮助文档. char的总结: char最大长度是255字符,注意是字符数和字符集没关系.可以有默认值,尾部有空格会被截断.varchar的总结 ...
- jquery easy ui 1.3.4 窗口,对话框,提示框(5)
5.1.window(窗口) 窗口我们在程序中会大量的使用,比如我们的添加.编辑都可以使用窗口实现,与winform的程序非常的类似.下面的代码是创建一个基本的窗口 $(function () { $ ...