Leetcode 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.
子数组乘积最大
class Solution {
public:
int maxProduct(int A[], int n) {
if(n < ) return ;
int minv = A[], maxv = A[], res = A[];
for(int i = ; i < n; ++ i){
int tmpMin = min(minv*A[i],maxv*A[i]);
int tmpMax = max(minv*A[i],maxv*A[i]);
minv = min(tmpMin,A[i]);
maxv = max(tmpMax,A[i]);
res=max(maxv,res);
}
return res;
};
Leetcode Maximum Product Subarray的更多相关文章
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关
Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 152.[LeetCode] Maximum Product Subarray
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- [LeetCode] Maximum Product Subarray 连续数列最大积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode]Maximum Product Subarray @ Python
原题地址:https://oj.leetcode.com/problems/maximum-product-subarray/ 解题思路:主要需要考虑负负得正这种情况,比如之前的最小值是一个负数,再乘 ...
- LeetCode Maximum Product Subarray 解题报告
LeetCode 新题又更新了.求:最大子数组乘积. https://oj.leetcode.com/problems/maximum-product-subarray/ 题目分析:求一个数组,连续子 ...
- LeetCode Maximum Product Subarray 最大子序列积
题意:给一个size大于0的序列,求最大的连续子序列之积.(有正数,负数,0) 思路:正确分析这三种数.0把不同的可能为答案的子序列给隔开了,所以其实可以以0为分隔线将他们拆成多个序列来进行求积,这样 ...
- DP Leetcode - Maximum Product Subarray
近期一直忙着写paper,非常久没做题,一下子把题目搞复杂了..思路理清楚了非常easy,每次仅仅需更新2个值:当前子序列最大乘积和当前子序列的最小乘积.最大乘积被更新有三种可能:当前A[i]> ...
随机推荐
- git+github上传与管理
1.首先下载并安装git,方便管理github上的代码 https://git-scm.com/downloads 2.然后点击安装好的git bash(注册好自己的github) git confi ...
- JSON Accelerator真是个好东西...
支持OBJC,JAVA,Python,OBJC(Core Data),Python(Django) 并实现了NSCoding和NSCoping,方便归档和复制. 再也不用辛辛苦苦的写Model了.. ...
- SQL SERVER 2008 获取表字段的类型
SELECT * FROM ( select a.name TABLENAME,b.name FIELDNAME,c.name FIELDTYPE,c.length FIELDLENGTH from ...
- 浅谈HTTP中Get与Post的区别
引用自:http://www.cnblogs.com/hyddd/archive/2009/03/31/1426026.html Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET ...
- Selenium使用
定位 1.普通 by id, name,class_name,link_text 2.加强 xpath css
- 小米4 miui专用 Xposed安装器86版
转载自 http://www.52pojie.cn/thread-516435-1-1.html 写在前面:各位用xp受到不同限制,有些机型还找不到框架包,又要刷第三方rec又要谨慎选择框架版本.官方 ...
- WEB 用户指南 -- WEB 系统结构文档
本文描述了如何使用 WEB语言 编程.同时还包含了 WEAVE 和 TANGLE 程序的说明文档. WEB 程序 可以读取 WEB 文件,然后输出 TeX 文档 和 Pascal 程序. 使用 WEB ...
- [Project Name] was compiled with optimization - stepping may behave oddly; variables may not be available.
控制台输出的时候显示一串这样的信息:[Project Name] was compiled with optimization - stepping may behave oddly; variabl ...
- iscroll.js 下拉刷新和上拉加载
html代码如下 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- JavaScript 嵌套 书名号 查询
字符串中查找嵌套书名号 ,一开始想用正则来做的,最后发现正则不可能达到我的需求(可能是我正则理解不够,哈哈),所以写下了这个方法:只做了三层,如果你要更多,可以自己添加,哈哈//提取查询关键字,sou ...