近期一直忙着写paper,非常久没做题,一下子把题目搞复杂了。。思路理清楚了非常easy,每次仅仅需更新2个值:当前子序列最大乘积和当前子序列的最小乘积。最大乘积被更新有三种可能:当前A[i]>0,乘曾经面最大的数(>0),得到新的最大乘积;当前A[i]<0,乘曾经面最小的数(<0),得到新的最大乘积;A[i]它自己>0,(A[i-1]==0。最小乘积同理。。

class Solution {
public: int Max(int a, int b, int c)
{
int max = -1 << 30; if (a >= b)
max = a;
else
max = b;
if (c > max)
max = c; return max;
} int Min(int a, int b, int c)
{
int min = 1 << 30; if (a <= b)
min = a;
else
min = b;
if (c < min)
min = c; return min;
} int maxProduct(int A[], int n) {
int maxPPrev=A[0], maxP;
int minPPrev = A[0], minP; int max = A[0]; for (int i = 1; i < n; i++)
{
maxP = Max(maxPPrev * A[i], minPPrev * A[i], A[i]);
minP = Min(maxPPrev * A[i], minPPrev * A[i], A[i]); maxPPrev = maxP;
minPPrev = minP; if (maxPPrev > max)
max = maxPPrev; if (minPPrev > max)
max = minPPrev;
} return max;
}
};

DP Leetcode - Maximum Product Subarray的更多相关文章

  1. LeetCode Maximum Product Subarray(枚举)

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

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

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

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

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

  4. [LeetCode] Maximum Product Subarray 连续数列最大积

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

  5. Leetcode Maximum Product Subarray

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

  6. 152.[LeetCode] Maximum Product Subarray

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  7. [leetcode]Maximum Product Subarray @ Python

    原题地址:https://oj.leetcode.com/problems/maximum-product-subarray/ 解题思路:主要需要考虑负负得正这种情况,比如之前的最小值是一个负数,再乘 ...

  8. LeetCode Maximum Product Subarray 解题报告

    LeetCode 新题又更新了.求:最大子数组乘积. https://oj.leetcode.com/problems/maximum-product-subarray/ 题目分析:求一个数组,连续子 ...

  9. LeetCode Maximum Product Subarray 最大子序列积

    题意:给一个size大于0的序列,求最大的连续子序列之积.(有正数,负数,0) 思路:正确分析这三种数.0把不同的可能为答案的子序列给隔开了,所以其实可以以0为分隔线将他们拆成多个序列来进行求积,这样 ...

随机推荐

  1. nginx subrequest演示示例程序

    只有简单subrequest应用演示示例. nginx.conf文件: #user nobody; worker_processes 1; #error_log logs/error.log; #er ...

  2. 搭建solr单机版

    solr单机版的搭建 一.solr单机版的搭建 1.运行环境 solr 需要运行在一个Servlet容器中,Solr4.10.3要求jdk使用1.7以上,Solr默认提供Jetty(ja),本教va写 ...

  3. 图像特效——摩尔纹 moir

    %%% Moir %%% 摩尔纹 clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algo ...

  4. Vue.js学习与理解

    Vue.js(读音 /vjuː/, 类似于 view)是一个构建数据驱动的 web 界面的库.Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件. Vue.js 自身不 ...

  5. go语言实现遍历目录,及查找特定的文件类型

    // filelist.go package main import ( //"flag" "fmt" "os" "path/fi ...

  6. SCM文章10课时:定时器中断

    JP3遇见P0口. #include<reg51.h> #define uchar unsigned char uchar flag,num; uchar code smg[10] = { ...

  7. C++学习笔记9-运算符重载

    1. 重载运营商必须有一个类类型的操作数 对于内置类型运营商.它的意义不能改变. 例如,内置整数加法运算不能被重新定义: // error: cannotredefine built-in opera ...

  8. Lua相关的知识

    http://stackoverflow.com/questions/5438751/how-to-debug-lua-remotely http://cn.bing.com/search?q=org ...

  9. lua-TestMore(转)

    http://fperrad.github.io/lua-TestMore/ http://www.softpedia.com/get/Programming/Debuggers-Decompiler ...

  10. C++ Primer 学习笔记_2_高速入口(继续)

     P15习题 //题1.14: 试分析假设v1 == v2的情况下,该程序的输出结果 #include <iostream> int main() { std::cout <&l ...