1 Maximum Product Subarray_Leetcode
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.
Since two negative numbers may multiply and get a large number, I need to track the minimum number of each position in the sequence.
At each point, the maximum can be obtained by three numbers:
1. A[i]
2. A[i] * curmax
3. A[i] * curmin
Similar as the minimum update.
FIRST TRY ERROR:
I should use temp variable to store the curmax, since the update of curmin use the previous value of curmax.
Code:
class Solution {
public:
int maxProduct(int A[], int n) {
if(n == 0 || A == NULL) return 0;
int curmax = A[0], curmin = A[0];
int res = A[0];
for(int i = 1; i < n; i++)
{
int tmpmax = curmax, tmpmin = curmin; // take care, curmax is used when calc curmin, so do not update
curmax = max(max(A[i], A[i]*tmpmax), A[i]*tmpmin);
curmin = min(min(A[i], A[i]*tmpmax), A[i]*tmpmin);
if(curmax > res) res = curmax;
}
return res;
}
};
1 Maximum Product Subarray_Leetcode的更多相关文章
- uva 11059 maximum product(水题)——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAB1QAAAMcCAIAAABo0QCJAAAgAElEQVR4nOydW7msuhKF2wIasIAHJK
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- Uva 11059 Maximum Product
注意long long long long longlong !!!!!! 还有 printf的时候 明明longlong型的答案 用了%d WA了也看不出,这个细节要注意!!! #incl ...
- LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关
Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...
- 最大乘积(Maximum Product,UVA 11059)
Problem D - Maximum Product Time Limit: 1 second Given a sequence of integers S = {S1, S2, ..., Sn}, ...
- 暴力求解——最大乘积 Maximum Product,UVa 11059
最大乘积 Maximum Product 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/B 解题思路 ...
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- LeetCode 628. Maximum Product of Three Numbers (最大三数乘积)
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
随机推荐
- C++构造函数
一. 构造函数是干什么的class Counter{public: // 类Counter的构造函数 // 特点:以类名作为函数名,无返回类型 Coun ...
- api接口类型
类型一:js+xml 类型二:纯php模式 参考: <?php $ip = '117.25.13.123'; $datatype = 'text'; $url = 'http://api.ip1 ...
- 在 .NET 中开发基于 Chrome 内核的浏览器-创建一个简单浏览器
首先在 http://www.cftea.com/tools/downloads/Cef.zip 下载文件包. 一.将文件解压拖入到 Visual Studio 对应的 WinForm 项目中. 二. ...
- Entity Framework 杂记
本系列文章,将介绍本人在学习和使用Entity Framewrok的过程中的收获与心得. 或许有的地方讲的错误 欢迎大家批评指出. 1.EntityFramework 数据库的迁移 2.Mysql 该 ...
- jQuery $(document).ready() 与window.onload的区别
ps:jQuery中$(document).ready()的作用类似于传统JavaScript中的window.onload方法,虽然具有类似的效果,但是它们在触发操作的时间上存在着微妙的差异. 在j ...
- NetBean 8 创建EJB
一. 介绍 百度了一下关于在NetBean开发环境里创建EJB的教程,没有找到好的例子,2天的调试过程,写下来帮助后人. EJB (Enterprise Java Bean) 是一套高扩展性的开发企业 ...
- iOS json 解析遇到error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed.
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 38 ...
- HttpCache缓存扩展方法
using System;using System.Collections;using System.Configuration;using System.Web;using System.Web.C ...
- PHP 正则表达式 修饰符
下面列出了当前可用的 PCRE 修饰符.括号中提到的名字是 PCRE 内部这些修饰符的名称. 模式修饰符中的空格,换行符会被忽略,其他字符会导致错误. i (PCRE_CASELESS) 如果设置了这 ...
- 转:C++项目中的extern "C" {}
引言 在用C++的项目源码中,经常会不可避免的会看到下面的代码: #ifdef __cplusplus extern "C" { #endif /*...*/ #ifdef __c ...