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的更多相关文章

  1. uva 11059 maximum product(水题)——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAB1QAAAMcCAIAAABo0QCJAAAgAElEQVR4nOydW7msuhKF2wIasIAHJK

  2. [LeetCode] Maximum Product Subarray 求最大子数组乘积

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  3. 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray

    题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...

  4. Uva 11059 Maximum Product

    注意long long  long long  longlong !!!!!!   还有 printf的时候 明明longlong型的答案 用了%d  WA了也看不出,这个细节要注意!!! #incl ...

  5. LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关

    Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...

  6. 最大乘积(Maximum Product,UVA 11059)

    Problem D - Maximum Product Time Limit: 1 second Given a sequence of integers S = {S1, S2, ..., Sn}, ...

  7. 暴力求解——最大乘积 Maximum Product,UVa 11059

    最大乘积 Maximum Product 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/B 解题思路 ...

  8. LeetCode Maximum Product Subarray(枚举)

    LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...

  9. LeetCode 628. Maximum Product of Three Numbers (最大三数乘积)

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

随机推荐

  1. ORM之殇,我们需要什么样的ORM框架?

    最近在研究ORM,究竟什么样的框架才是我们想要的 开发框架的意义在于 开发更标准,更统一,不会因为不同人写的代码不一样 开发效率更高,无需重新造轮子,重复无用的代码,同时简化开发流程 运行效率得到控制 ...

  2. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' heade

    XMLHttpRequest cannot load http://10.164.153.37:8050/WebService/WebService.asmx/wsGetStreetData. Res ...

  3. LUA 学习笔记

    1.C# 与 LUAC#调用LUA比较简单,但LUA调用C#,有两种方法,一种是直接反射调用,但这种方法有局限性,比如性能低,在IOS平台无法使用反射,因此一般使用WARP方法,即把C#代码注册到LU ...

  4. Activiti5.10简易教程一

    Activiti5.10简易教程一 一搭建环境 1.1   JDK 6+ activiti 运行在版本 6 以上的 JDK 上.转到 Oracle Java SE 下载页面,点击按钮“下载 JDK ” ...

  5. Altium Designer 的entry sheet ,offsheet和port作用(转载)

    1.图纸结构 图纸包括两种结构关系: 一种是层次式图纸,该连接关系是纵向的,也就是某一层次的图纸只能和相邻的上级或下级有关系: 另一种是扁平式图纸,该连接关系是横向的,任何两张图纸之间都可以建立信号连 ...

  6. java基础 绘图技术.坦克大战 之java绘图坐标体系(二)

    功能:在坐标系上绘制坦克 /* * 功能:坦克游戏的1.0 * 1. 画出坦克 * */ package com.tank; import javax.swing.*; import java.awt ...

  7. java基础 super 子类调用父类

    如果希望在子类中,去调用父类的构造方法,要求在子类的构造函数调用 example如下: package test; /* * 如果希望在子类中,去调用父类的构造方法,要求在子类的构造函数调用 * */ ...

  8. oracle dump数据库

    最近正在看老白的<DBA的思想天空>,了解数据块结构,想通过dump data block验证oracle对于行尾的NULL,是不占用存储空间的. 我们先来看一下怎样dump数据块: 1. ...

  9. 转:CentOS/Debian/Ubuntu一键安装LAMP(Apache/MySQL/PHP)环境

    CentOS/Debian/Ubuntu一键安装LAMP(Apache/MySQL/PHP) 今天遇到一个网友提到需要在Linux VPS服务器中安装LAMP(Apache/MySQL/PHP)网站环 ...

  10. Mantis搭建步骤

    (1)安装EeasyPHP (2)解压Mantis到EeasyPHP内www目录下 (3)将PHP复制到www目录下 并修改apache下httpd.conf及php.ini两个文件的php配置目录 ...